Skip to content

Commit 7e3fc93

Browse files
feat: combination-sum ํ’€์ด
1 parent a3da5a1 commit 7e3fc93

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* @param {number[]} candidates
3+
* @param {number} target
4+
* @return {number[][]}
5+
*/
6+
7+
/**
8+
* candidates๋ฅผ ์—ฌ๋Ÿฌ๋ฒˆ ์‚ฌ์šฉํ•ด๋„ ๋œ๋‹ค. ํ•˜์ง€๋งŒ ์‚ฌ์šฉํ•œ ์ข…๋ฅ˜์™€ ๊ฐœ์ˆ˜๊ฐ€ ๋ชจ๋‘ ๊ฐ™์œผ๋ฉด ์•ˆ๋œ๋‹ค.
9+
*
10+
*/
11+
var combinationSum = function(candidates, target) {
12+
const candidatesLength = candidates.length
13+
// ์‹œ์ž‘ ์‹œ์ ์—์„œ array๋ฅผ ๋งŒ๋“ค์–ด์„œ ๋„˜๊ฒจ์ค˜์•ผํ•œ๋‹ค.
14+
// candidates ์ค‘์— ๋„ฃ์œผ๋ฉด target์ด ๋˜๋ฉด ๋‹ต์— ๊ทธ ๋ฐฐ์—ด์„ ๋„ฃ๊ณ  ํƒˆ์ถœํ•œ๋‹ค.
15+
// ๋งŒ์•ฝ ๋”ํ–ˆ๋Š”๋ฐ target๋ณด๋‹ค ํฌ๋ฉด ๊ทธ๋ƒฅ ๋ฆฌํ„ดํ•œ๋‹ค.
16+
// target๋ณด๋‹ค ์ž‘์€๊ฒƒ์— ๋Œ€ํ•ด์„œ ๋„ฃ๋Š”๋‹ค.
17+
const ret = []
18+
function dp(total,newNum,currentArr){
19+
if(total + newNum > target){
20+
return
21+
}
22+
if(total + newNum == target && ){
23+
ret.push([...currentArr,newNum]);
24+
return
25+
}
26+
27+
//๋‘ ๊ฐ€์ง€ ์–ด๋А๊ฒƒ์—๋„ ํ•ด๋‹นํ•˜์ง€ ์•Š์œผ๋ฉด ์žฌ๊ท€๋ฅผ ๋˜ ๋‹ค์‹œ ๋ˆ๋‹ค.
28+
for(let i = 0; i<candidatesLength; i++){
29+
dp(total+newNum , candidates[i],[...currentArr,candidates[i]])
30+
}
31+
}
32+
dp(0,0,[])
33+
return ret
34+
};
35+
36+
//์—ฌ๊ธฐ์„œ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ฒผ๋‹ค.
37+
// ์ค‘๋ณต์ด ์ƒ๊ธด๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์–ด๋–ป๊ฒŒ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๋ฉด ์ข‹์„๊นŒ?
38+
/**
39+
* 1. Set์„ ์ด์šฉํ•œ๋‹ค.
40+
* 2. ์ˆœ์„œ๋ฅผ ๋‘”๋‹ค. idx๋ฅผ dfs์— ๋„˜๊ฒจ์ฃผ์–ด์„œ, ์ˆœ์„œ๋Œ€๋กœ ์ž‘๋™ํ•˜๊ฒŒ ํ•œ๋‹ค.
41+
*/
42+
43+
var combinationSum = function(candidates, target) {
44+
const candidatesLength = candidates.length
45+
const ret = []
46+
function dp(idx,total,currentArr){
47+
if(total > target){
48+
return
49+
}
50+
if(total == target){
51+
ret.push(currentArr);
52+
return
53+
}
54+
55+
//idx๋ฅผ ๋„˜๊ฒจ๋ฐ›์•„์„œ, ๊ทธ ์ดํ›„์˜ ๊ฒƒ์—๋งŒ(์ž๊ธฐ ์ž์‹  ํฌํ•จ) ๋Œ๊ฒŒ ๋œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์ ˆ๋ฐ˜์ •๋„๋งŒ ๋ˆ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
56+
for(let i = idx; i < candidatesLength; i++){
57+
dp(i,total+ candidates[i],[...currentArr,candidates[i]])
58+
}
59+
}
60+
dp(0,0,[])
61+
return ret
62+
};
63+
64+
65+
66+
/**
67+
์‹œ๊ฐ„๋ณต์žก๋„: O(N^(target/min)) : N๊ฐœ ํ›„๋ณด ์ค‘ ์„ ํƒํ•˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ target/min ๊นŠ์ด๋งŒํผ ๋ฐ˜๋ณต
68+
๊ณต๊ฐ„๋ณต์žก๋„: O(target/min) : ์žฌ๊ท€ ์Šคํƒ์˜ ์ตœ๋Œ€ ๊นŠ์ด๊ฐ€ target/min
69+
70+
71+
*/
72+
73+
74+
// 2. dp๋ฅผ ํ™œ์šฉํ•œ ๋ฌธ์ œ ํ•ด๊ฒฐ๋ฐฉ๋ฒ•
75+
var combinationSum = function(candidates, target) {
76+
const dp = Array.from({length:target+1},()=>[])
77+
dp[0] = [[]];
78+
for(let candidate of candidates){
79+
//์—ฌ๊ธฐ์„œ candidate์— ๋Œ€ํ•ด์„œ, ์ดํ›„ ํƒ€๊ฒŸ๊นŒ์ง€ ๋”ํ•  ์ˆ˜์žˆ๋Š” ์ˆซ์ž์— ๋Œ€ํ•œ ์กฐํ•ฉ์—, candidate๋ฅผ ๊ฐ๊ฐ ๋„ฃ์–ด์ค€๋‹ค.
80+
for(let i = candidate; i <= target; i++){
81+
for(let comb of dp[i-candidate]){ //๊ฐ ์กฐํ•ฉ์— ๋„ฃ์–ด์คŒ
82+
dp[i].push([...comb,candidate])
83+
}
84+
}
85+
}
86+
return dp[target]
87+
};
88+
/**
89+
์‹œ๊ฐ„๋ณต์žก๋„: O(N ร— target ร— 2^target)
90+
N๊ฐœ ํ›„๋ณด๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ, ๊ฐ ํ›„๋ณด๋งˆ๋‹ค target๊นŒ์ง€์˜ ๊ฐ’๋“ค์— ๋Œ€ํ•ด ๊ธฐ์กด ์กฐํ•ฉ๋“ค์„ ๋ณต์‚ฌํ•ด์„œ ์ƒˆ ์กฐํ•ฉ์„ ์ƒ์„ฑํ•˜๋Š”๋ฐ, ์กฐํ•ฉ ๊ฐœ์ˆ˜๊ฐ€ ์ง€์ˆ˜์ ์œผ๋กœ ์ฆ๊ฐ€ํ•จ
91+
๊ณต๊ฐ„๋ณต์žก๋„: O(target ร— 2^target)
92+
dp ๋ฐฐ์—ด์˜ ๊ฐ ์ธ๋ฑ์Šค์— ํ•ด๋‹น ๊ฐ’์„ ๋งŒ๋“œ๋Š” ๋ชจ๋“  ์กฐํ•ฉ๋“ค์„ ์ €์žฅํ•˜๋ฉฐ, ์ตœ์•…์˜ ๊ฒฝ์šฐ ์กฐํ•ฉ ๊ฐœ์ˆ˜๊ฐ€ ์ง€์ˆ˜์ ์œผ๋กœ ์ฆ๊ฐ€ํ•จ
93+
*/

0 commit comments

Comments
ย (0)