Skip to content

Commit 145dbc2

Browse files
committed
combination-sum solution
1 parent fb90368 commit 145dbc2

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

β€Žcombination-sum/Blossssom.tsβ€Ž

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @param - candidates: μ •μˆ˜ λ°°μ—΄, target: λŒ€μƒ μ •μˆ˜
3+
* @returns - μ„ νƒν•œ 숫자의 합이 ν•΄λ‹Ήν•˜λŠ” λͺ¨λ“  고유 μ‘°ν•© λ°˜ν™˜
4+
* @description
5+
* 1. μš”μ†ŒλŠ” μ—¬λŸ¬λ²ˆ μ‚¬μš© κ°€λŠ₯
6+
*/
7+
8+
function combinationSum(candidates: number[], target: number): number[][] {
9+
const result: number[][] = [];
10+
11+
function recursive(remain: number, idx: number, path: number[]) {
12+
if (remain === 0) {
13+
result.push([...path]);
14+
return;
15+
}
16+
17+
for (let i = idx; i < candidates.length; i++) {
18+
const currentNum = candidates[i];
19+
if (currentNum > remain) {
20+
break;
21+
}
22+
23+
path.push(currentNum);
24+
recursive(remain - currentNum, i, path);
25+
path.pop();
26+
}
27+
}
28+
29+
recursive(target, 0, []);
30+
return result;
31+
}
32+
33+
const candidates = [2, 3, 5];
34+
const target = 8;
35+
combinationSum(candidates, target);

0 commit comments

Comments
Β (0)