Watch 10 video solutions for Combination Sum IV, a medium level problem involving Array, Dynamic Programming. This walkthrough by NeetCode has 49,625 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
Example 1:
Input: nums = [1,2,3], target = 4 Output: 7 Explanation: The possible combination ways are: (1, 1, 1, 1) (1, 1, 2) (1, 2, 1) (1, 3) (2, 1, 1) (2, 2) (3, 1) Note that different sequences are counted as different combinations.
Example 2:
Input: nums = [9], target = 3 Output: 0
Constraints:
1 <= nums.length <= 2001 <= nums[i] <= 1000nums 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?
Problem Overview: You are given an array of distinct integers and a target value. The task is to count how many different ordered combinations of numbers sum to the target. Order matters, so [1,2,1] and [1,1,2] are treated as different combinations. This turns the problem into counting permutations with repetition using array traversal and dynamic programming.
Approach 1: Dynamic Programming with Memoization (Top-Down) (Time: O(n × target), Space: O(target))
This approach models the problem as a recursive decision tree. For a remaining sum t, you try every number in the array and recursively compute the number of ways to form t - num. The key observation: many subproblems repeat. For example, the number of ways to build sum 4 may be computed multiple times during recursion.
Memoization fixes this by caching results in a map or array indexed by the remaining target. Each state dp[t] represents the number of ordered combinations that produce sum t. When recursion revisits the same state, the cached value is returned instantly instead of recomputing the subtree. The recursion stops when t == 0 (one valid combination) or t < 0 (invalid path).
This top‑down strategy mirrors how you naturally reason about the problem. It is easy to implement and clearly exposes the overlapping subproblems typical in dynamic programming.
Approach 2: Dynamic Programming Using Tabulation (Bottom-Up) (Time: O(n × target), Space: O(target))
The bottom‑up version removes recursion and builds the answer iteratively. Create a DP array where dp[i] represents the number of ordered combinations that sum to value i. Initialize dp[0] = 1 because there is exactly one way to form sum 0: choose nothing.
Iterate from 1 to target. For each sum i, iterate through every number in the input array. If num ≤ i, then any combination that forms i - num can be extended with num to form i. Add dp[i - num] to dp[i]. This effectively counts permutations because the outer loop iterates over target sums while the inner loop tries every element of the array.
The tabulation method is typically faster in practice since it avoids recursion overhead and stack usage. It also makes the state transition very explicit: each state aggregates results from smaller sums.
Recommended for interviews: The bottom‑up tabulation solution is usually what interviewers expect. It demonstrates a clear DP state definition and efficient iteration with O(n × target) time and O(target) space. Starting with the memoized recursion can still be useful during discussion because it shows how you derive the recurrence before converting it into an iterative DP.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Dynamic Programming with Memoization (Top-Down) | O(n × target) | O(target) | When deriving the recurrence relation or explaining the recursion tree in interviews |
| Dynamic Programming with Tabulation (Bottom-Up) | O(n × target) | O(target) | Preferred production and interview solution; avoids recursion overhead and is easy to reason about |