Skip to content

Commit af374c9

Browse files
committed
ํ’€์ด1: Combination Sum DaleStudy#254
1 parent 184be39 commit af374c9

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

โ€Žcombination-sum/seungseung88.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* ์‹œ๊ฐ„๋ณต์žก๋„: O(2^n)
3+
* ๊ณต๊ฐ„๋ณต์žก๋„: O(target)
4+
*/
5+
6+
function combinationSum(canditates, target) {
7+
const result = [];
8+
const nums = [];
9+
10+
function dfs(start, total) {
11+
if (total > target) return;
12+
if (total === target) result.push([...nums]);
13+
14+
// ์ค‘๋ณต์ œ๊ฑฐ๋ฅผ ์œ„ํ•ด start๋กœ ์‹œ์ž‘
15+
for (let i = start; i < canditates.length; i += 1) {
16+
num = canditates[i];
17+
nums.push(num);
18+
dfs(i, total + num);
19+
nums.pop();
20+
}
21+
}
22+
23+
// ์‹œ์ž‘ ์ธ๋ฑ์Šค, ๋ˆ„์  ํ•ฉ
24+
dfs(0, 0);
25+
26+
return result;
27+
}

0 commit comments

Comments
ย (0)