Skip to content

Commit 0e7bd78

Browse files
committed
combination-sum solution
1 parent 4ab5a03 commit 0e7bd78

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number[]} candidates
3+
* @param {number} target
4+
* @return {number[][]}
5+
*/
6+
var combinationSum = function(candidates, target) {
7+
const result = [];
8+
9+
const dfs = (start, path, sum) => {
10+
if (sum === target) {
11+
result.push([...path]); // ์ •๋‹ต ์กฐํ•ฉ ๋ฐœ๊ฒฌ
12+
return;
13+
}
14+
15+
if (sum > target) return; // target ์ดˆ๊ณผ -> ๋ฐฑํŠธ๋ž™
16+
17+
for (let i = start; i < candidates.length; i++) {
18+
path.push(candidates[i]); // ์ˆซ์ž ์„ ํƒ
19+
dfs(i, path, sum + candidates[i]); // ๊ฐ™์€ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ๋‹ค์‹œ ํƒ์ƒ‰ (์ค‘๋ณต ์‚ฌ์šฉ ํ—ˆ์šฉ)
20+
path.pop(); // ๋ฐฑํŠธ๋ž˜ํ‚น
21+
}
22+
};
23+
24+
dfs(0, [], 0);
25+
return result;
26+
};

0 commit comments

Comments
ย (0)