Skip to content

Commit 95cc89b

Browse files
committed
combination sum solution
1 parent 94bf9b6 commit 95cc89b

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

combination-sum/robinyoon-dev.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @param {number[]} candidates
3+
* @param {number} target
4+
* @return {number[][]}
5+
*/
6+
var combinationSum = function (candidates, target) {
7+
8+
const result = [];
9+
10+
function dfs(remainTarget, currentArray, currentIndex) {
11+
12+
if (remainTarget < 0) {
13+
return;
14+
}
15+
16+
if (remainTarget == 0) {
17+
result.push([...currentArray]);
18+
return;
19+
}
20+
21+
// 백트래킹 탐색
22+
for (let i = currentIndex; i < candidates.length; i++) {
23+
currentArray.push(candidates[i]);
24+
25+
dfs(remainTarget - candidates[i], currentArray, i);
26+
27+
currentArray.pop();
28+
}
29+
30+
}
31+
32+
dfs(target, [], 0);
33+
34+
return result;
35+
36+
};

0 commit comments

Comments
 (0)