Skip to content

Commit 191748f

Browse files
committed
week3: combination-sum
1 parent 82e6ecb commit 191748f

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

combination-sum/reeseo3o.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const combinationSum = (candidates, target) => {
2+
const result = [];
3+
4+
const backtrack = (startIndex, current, remaining) => {
5+
if (remaining === 0) {
6+
result.push([...current]);
7+
return;
8+
}
9+
if (remaining < 0) return;
10+
11+
for (let i = startIndex; i < candidates.length; i++) {
12+
current.push(candidates[i]);
13+
backtrack(i, current, remaining - candidates[i]);
14+
current.pop();
15+
}
16+
};
17+
18+
backtrack(0, [], target);
19+
return result;
20+
};

0 commit comments

Comments
 (0)