Skip to content

Commit 2ed50ad

Browse files
committed
solve combination-sum
1 parent 7773d46 commit 2ed50ad

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

combination-sum/1lsang.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function combinationSum(candidates: number[], target: number): number[][] {
2+
// arr: index까지 갈 수 있는 combinationSum
3+
const arr:number[][][] = Array.from({ length: target + 1 }, () => [] as number[][]);
4+
// 0을 만들 수 있는 방법은 숫자가 없는 것
5+
arr[0].push([] as number[]);
6+
7+
for (const candidate of candidates) {
8+
for (let n = candidate; n <= target; n++) {
9+
for (const combination of arr[n-candidate]) {
10+
arr[n].push([...combination, candidate]);
11+
}
12+
}
13+
}
14+
console.log(arr);
15+
return arr[target];
16+
};

0 commit comments

Comments
 (0)