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 83f3bbb commit ee47e47Copy full SHA for ee47e47
โcombination-sum/moonjonghoo.jsโ
@@ -0,0 +1,28 @@
1
+function combinationSum(candidates, target) {
2
+ const result = [];
3
+
4
+ // 1. ์ ๋ ฌ: ๊ฐ์ง์น๊ธฐ๋ฅผ ์ํ ํ์
5
+ candidates.sort((a, b) => a - b);
6
7
+ function backtrack(startIndex, path, remaining) {
8
+ if (remaining === 0) {
9
+ result.push([...path]);
10
+ return;
11
+ }
12
13
+ for (let i = startIndex; i < candidates.length; i++) {
14
+ const current = candidates[i];
15
16
+ // 2. ๊ฐ์ง์น๊ธฐ
17
+ if (current > remaining) break;
18
19
+ // 3. ํ์ฌ ๊ฐ ์ ํ
20
+ path.push(current);
21
+ backtrack(i, path, remaining - current); // i๋ก ์ฌ๊ท ํธ์ถ: ๊ฐ์ ์ ์ค๋ณต ์ฌ์ฉ ๊ฐ๋ฅ
22
+ path.pop(); // 4. ๋ฐฑํธ๋ํน
23
24
25
26
+ backtrack(0, [], target);
27
+ return result;
28
+}
0 commit comments