We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 4ab5a03 commit 0e7bd78Copy full SHA for 0e7bd78
โcombination-sum/krokerdile.jsโ
@@ -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