Skip to content

Commit 3ad73ad

Browse files
committed
Combination Sum
1 parent 4f00340 commit 3ad73ad

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

combination-sum/casentino.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function combinationSum(candidates: number[], target: number): number[][] {
2+
const results: number[][] = [];
3+
4+
function comb(index: number, arr: number[], sum: number) {
5+
if (sum === target) {
6+
results.push([...arr]);
7+
return;
8+
}
9+
if (sum > target || candidates.length <= index) {
10+
return;
11+
}
12+
arr.push(candidates[index]);
13+
comb(index, arr, sum + candidates[index]);
14+
arr.pop();
15+
comb(index + 1, arr, sum);
16+
}
17+
18+
comb(0, [], 0);
19+
return results;
20+
}

0 commit comments

Comments
 (0)