LeetCode 377, Medium. Topics: Array, Dynamic Programming. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 15 parametrized pytest cases, and a playground notebook:
Problem
Given an array of distinct integers nums and a target integer target, return the number of possible combinations that add up to target.
The test cases are generated so that the answer can fit in a 32-bit integer.
Note that different sequences are counted as different combinations.
Examples
Constraints
- 1 <= nums.length <= 200
- 1 <= nums[i] <= 1000
- All the elements of
nums are unique.
- 1 <= target <= 1000
Follow up: What if negative numbers are allowed in the given array? How does it change the problem? What limitation we need to add to the question to allow negative numbers?
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
Grind, NeetCode 250, NeetCode All. Last modified on September 7, 2026