L377. Combination Sum IV
dp
Problem:
Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.
Example:
nums = [1, 2, 3]
target = 4
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.
Therefore the output is 7.
Solution:
Imagine we only need one more number to reach target, this number can be any one in the array, right? So the # of combinations of
target
,comb[target] = sum(comb[target - nums[i]]), where 0 <= i < nums.length, and target >= nums[i]
In the example given, we can actually find the # of combinations of 4 with the # of combinations of 3(4 - 1), 2(4- 2) and 1(4 - 3). As a result,
comb[4] = comb[4-1] + comb[4-2] + comb[4-3] = comb[3] + comb[2] + comb[1]
Then think about the base case. Since if the target is 0, there is only one way to get zero, which is using 0, we can set
comb[0] = 1
.
public int combinationSum4(int[] nums, int target) {
if (target == 0) {
return 1;
}
int res = 0;
for (int i = 0; i < nums.length; i++) {
if (target >= nums[i]) {
res += combinationSum4(nums, target - nums[i]);
}
}
return res;
}
Last updated
Was this helpful?