Skip to content

Commit cf6e2dc

Browse files
committed
add: combinationSum solution
1 parent f886f0b commit cf6e2dc

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// ! ๋‚˜์ค‘์— ๋‹ค์‹œ ํ’€์–ด์•ผ ํ•  ๋ฌธ์ œ
2+
//! 30๋ถ„ ๋‚ด์— ํ’€์ง€ ๋ชปํ•ด์„œ AI์˜ ๋„์›€์„ ๋ฐ›์•„ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค..
3+
// recursion์˜ ์‚ฌ์šฉ, ์ข…๋ฃŒ ์กฐ๊ฑด ๋“ฑ ์–ด๋А์ •๋„ ๊ทผ์ ‘ํ•œ ์•„์ด๋””์–ด๋ฅผ ๊ตฌํ˜„ํ–ˆ์ง€๋งŒ,
4+
// idx๋ฅผ ๋„˜๊ธฐ์ง€ ์•Š๊ณ  number๋ฅผ ์ง์ ‘ ๋„˜๊ธฐ๋ ค๊ณ  ํ–ˆ๋˜ ๋ถ€๋ถ„์ด๋‚˜, results pop์„ ๋†“์ณค๋˜ ๋ถ€๋ถ„ ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
5+
const combinationSum = function (candidates, target) {
6+
const answer = [];
7+
8+
function recursion(idx, target, results = []) {
9+
if (target === 0) {
10+
answer.push([...results]);
11+
return;
12+
}
13+
if (target < 0) return;
14+
15+
for (let i = idx; i < candidates.length; i++) {
16+
results.push(candidates[i]);
17+
recursion(i, target - candidates[i], results);
18+
results.pop();
19+
}
20+
}
21+
recursion(0, target, []);
22+
23+
return answer;
24+
};

0 commit comments

Comments
ย (0)