-
Notifications
You must be signed in to change notification settings - Fork 65
/
组合总和.js
53 lines (45 loc) · 1.03 KB
/
组合总和.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* @param {number[]} nums
* @param {number} target
* @return {number}
377. 组合总和 Ⅳ
target = 4
dp[4] 和为4的组合数
dp[4] = dp[3]+1 + dp[2]+2 + dp[1]+3 + dp[0]+4
[
[1,1,1]
[1,2]
]
输入:nums = [1,2,3], target = 4
d[4] = dp[3]+1 + dp[2]+2 + dp[1]+3 + dp[0]+4 (4没有出现)
4 + 2 + 1 = 7
d[3] = dp[2]+1 + dp[1]+2 + dp[0]+3
2 + 1 + 1 = 4
dp[2] = dp[1]+1 + dp[0]+2
1 + 1 = 2
dp[1] = dp[0]+1 // dp[1] = 1
1 = 1
dp[0] = 1
输入:nums = [1,2,3], target = 4
dp[4] = 4 + 2 + 1 = 7
= dp[3] + dp[2] + dp[1] = 7
dp[3] = 2 + 1 + 1 = 4
= dp[2] + dp[1] + dp[0] = 4
dp[2] = 1 + 1 = 2
= dp[1] + dp[0] = 2
dp[1] = 1
= dp[0] = 1
dp[0] = 1
*/
var combinationSum4 = function(nums, target) {
let dp = new Array(target+1).fill(0);
dp[0] = 1;
for(let i = 1; i<=target; i++) {
for(let j = 0; j < nums.length; j++) {
if(i - nums[j] >= 0) {
dp[i] += dp[i-nums[j]];
}
}
}
return dp[target];
};