Skip to content

Commit 4951f54

Browse files
committed
add: Combination Sum solution
1 parent b64ded9 commit 4951f54

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// [39] Combination Sum
2+
3+
function combinationSum(candidates: number[], target: number): number[][] {
4+
const n = candidates.length;
5+
const output: number[][] = [];
6+
7+
function dfs(arr: number[], startIdx: number, sum: number) {
8+
if (sum === target) {
9+
output.push([...arr]);
10+
return;
11+
}
12+
if (sum > target) return;
13+
14+
// ํ˜„์žฌ ์ˆซ์ž๋ถ€ํ„ฐ ๋‹ค์‹œ ์‹œ์ž‘ (์ค‘๋ณต ์„ ํƒ์ด ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ)
15+
for (let idx = startIdx; idx < n; idx++) {
16+
arr.push(candidates[idx]);
17+
dfs(arr, idx, sum + candidates[idx]);
18+
arr.pop();
19+
}
20+
}
21+
22+
dfs([], 0, 0);
23+
24+
return output;
25+
}

0 commit comments

Comments
ย (0)