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.
给出正数不重复的数组,找出能达到和为target的组合数,数字可以重复使用。
思路:
刚开始用dfs解,因为可以重复使用数字,最后TLE,需要转换DP思路
建立一个DP数组,长度是target+1,表示从0到target之间每个小的target有几种组合。dp[i]表示target=i的组合数。
选一个数字num,只需知道target-num有几种组合,而target-num的组合数保存在dp数组里就可以避免重复计算
dp[i] 是nums中所有数字的组合数之和。所以每算一个dp[i], 都要循环一次nums数组。
dp处理边界:target<0时,因为数组里全是正数,无法完成,所以返回0
target=0时,只需什么数字都不选,所以返回1
为避免0和未处理过的情况混淆,dp数组初始化为-1
而整个题target是正数,但是还是加入判断target=0的情况
public int combinationSum4(int[] nums, int target) {
if (target == 0) {
return 1;
}
int[] dp = new int[target + 1];
Arrays.fill(dp, -1);
dp[0] = 1;
return count(nums, target, dp);
}
public int count(int[] nums, int target, int[] dp) {
if (target < 0) {
return 0;
}
int sum = 0;
if (dp[target] != -1) {
return dp[target];
}
for (int i = 0; i < nums.length; i++) {
sum += count(nums, target - nums[i], dp);
}
dp[target] = sum;
return dp[target];
}