-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path39-combination-sum.js
73 lines (64 loc) · 1.22 KB
/
39-combination-sum.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
var combinationSum = comboSum;
function comboSumOriginal(c, t) {
const C = c.length;
c = c.map(n => parseInt(n));
c.sort((a,b) => b-a); // decreasing order
const m = [];
let i = 0;
while(i < C) {
const sum = [];
let N = t;
let j = i;
while(j < C) {
const part = c[j];
while(part <= N) {
N -= part;
sum.push(part);
m.push(...combinationSum(c.slice(j+1), N).map(x => (x.push(...sum), x)));
}
j++;
}
if ( N === 0 ) {
m.push(sum);
}
i++;
}
const dup = new Set();
m.forEach(x => x.sort());
const result = m.filter(y => {
const key = y.join(',');
if ( !dup.has(key) ) {
dup.add(key);
return true;
}
return false;
});
return result;
};
function cSum(t, c, i, S, s, A) {
if ( S > t ) return;
if ( S === t ) {
A.push(Array.from(s));
return;
}
for( ; i < c.length; i++ ) {
const C = c[i];
s.push(C);
S += C;
cSum(t, c, i, S, s, A);
s.pop();
S -= C;
}
}
function comboSum(c, t) {
const A = [];
const s = [];
cSum(t, c, 0, 0, s, A);
return A;
}
const T = [
combinationSum([2], 1),
combinationSum([2,3,5], 8),
combinationSum([2,3,6,7], 7),
];
console.log(T);