Skip to content

Commit 69fcd06

Browse files
committed
combination sum solution commit
1 parent 828972b commit 69fcd06

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

combination-sum/juhui-jeong.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// 시간 복잡도: O(2^(target / minCandidate))
2+
// 공간 복잡도: O(target / minCandidate)
3+
function combinationSum(candidates: number[], target: number): number[][] {
4+
const result: number[][] = [];
5+
6+
const dfs = (index: number, remain: number, path: number[]) => {
7+
if (remain === 0) {
8+
result.push([...path]);
9+
return;
10+
}
11+
12+
if (remain < 0 || index === candidates.length) {
13+
return;
14+
}
15+
16+
const num = candidates[index];
17+
18+
path.push(num);
19+
dfs(index, remain - num, path);
20+
path.pop();
21+
22+
dfs(index + 1, remain, path);
23+
};
24+
25+
dfs(0, target, []);
26+
27+
return result;
28+
}

0 commit comments

Comments
 (0)