Skip to content

Commit 6acd81c

Browse files
committed
solve combination-sum [dfs]
1 parent f322c64 commit 6acd81c

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

combination-sum/JangAyeon.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[]} candidates
3+
* @param {number} target
4+
* @return {number[][]}
5+
*/
6+
var combinationSum = function (arr, target) {
7+
const N = arr.length;
8+
const answer = [];
9+
function dfs(total, idx, route) {
10+
if (total >= target) {
11+
if (total == target) {
12+
answer.push(route);
13+
}
14+
return;
15+
}
16+
for (let i = idx; i < N; i++) {
17+
dfs(total + arr[i], i, [...route, arr[i]]);
18+
}
19+
}
20+
21+
dfs(0, 0, []);
22+
return answer;
23+
};

0 commit comments

Comments
 (0)