-
Notifications
You must be signed in to change notification settings - Fork 0
/
377.js
35 lines (32 loc) · 1.05 KB
/
377.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
var combinationSum4 = function(nums, target) {
// when different sequences are counted as the same combinations
// let comb = {[target]:1};
// for(let i=0;i<nums.length;i++){
// let maxNum = Math.floor(target/nums[i]);
// let targetNum = comb.length;
// for(let t in comb){
// let cnt = comb[t];
// delete comb[t];
// for(let j=0;j<=maxNum;j++){
// let rest = parseInt(t)-j*nums[i];
// if(rest<0)break;
// comb[rest]=comb[rest]||0;
// comb[rest]+=cnt;
// }
// }
// }
// return comb[0];
let memo = [1];
return helper(nums,target,memo);
};
var helper = function(nums,target,memo){
if(memo[target]!==undefined)return memo[target];
let ans = 0;
for(let i=0;i<nums.length;i++){
if(target-nums[i]<0)continue;
helper(nums,target-nums[i],memo);
ans+=memo[target-nums[i]];
}
memo[target] = ans;
return memo[target]
}