Skip to content

Commit 03ff9c5

Browse files
committed
add solution: combination-sum
1 parent 608c4d7 commit 03ff9c5

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

โ€Žcombination-sum/ZetBe.pyโ€Ž

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'''
2+
๋ฌธ์ œ: target์ด ๋  ๋•Œ๊นŒ์ง€ candidates์˜ ์ˆ˜๋ฅผ ๋”ํ•˜๋Š” ๋ชจ๋“  ์กฐํ•ฉ์„ ๊ตฌํ•˜๋ผ.
3+
ํ’€์ด: ๋ฐฑํŠธ๋ž˜ํ‚น์„ ์ด์šฉํ•˜์—ฌ ์กฐํ•ฉ์„ ๊ตฌํ•œ๋‹ค. ๋งŒ์•ฝ ํ˜„์žฌ ํ•ฉ์ด target๊ณผ ๊ฐ™๋‹ค๋ฉด ๊ฒฐ๊ณผ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•˜๊ณ , ์ž‘๋‹ค๋ฉด ํ›„๋ณด๊ตฐ์„ ์ˆœํšŒํ•˜๋ฉฐ ์žฌ๊ท€์ ์œผ๋กœ ํƒ์ƒ‰ํ•œ๋‹ค.
4+
์‹œ๊ฐ„๋ณต์žก๋„: O(N^T), N์€ ํ›„๋ณด๊ตฐ์˜ ์ˆ˜, T๋Š” target์˜ ๊ฐ’
5+
์ตœ์•…์˜ ๊ฒฝ์šฐ, ํ›„๋ณด๊ตฐ์˜ ์ˆ˜ N๊ณผ target์˜ ๊ฐ’ T์— ๋”ฐ๋ผ ๋ชจ๋“  ์กฐํ•ฉ์„ ํƒ์ƒ‰ํ•ด์•ผ ํ•˜๋ฏ€๋กœ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(N^T)์ด๋‹ค.
6+
๊ณต๊ฐ„๋ณต์žก๋„: O(T)
7+
์žฌ๊ท€ ํ˜ธ์ถœ ์Šคํƒ๊ณผ ํ˜„์žฌ ์กฐํ•ฉ์„ ์ €์žฅํ•˜๋Š” ๋ฐ ์ตœ๋Œ€ T๊ฐœ์˜ ๊ณต๊ฐ„์ด ํ•„์š”ํ•˜๋ฏ€๋กœ ๊ณต๊ฐ„๋ณต์žก๋„๋Š” O(T)์ด๋‹ค.
8+
์‚ฌ์šฉํ•œ ์ž๋ฃŒ๊ตฌ์กฐ: ๋ฆฌ์ŠคํŠธ
9+
์ฐธ๊ณ ๋กœ ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ ๊นŠ์ด๋Š” target/min(candidates)๋กœ ์ œํ•œ๋œ๋‹ค.
10+
'''
11+
12+
13+
class Solution:
14+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
15+
candidates.sort()
16+
s, now = [], []
17+
18+
def find(start, score):
19+
if score == target:
20+
s.append(now[:])
21+
elif score < target:
22+
for i in range(start, len(candidates)):
23+
n = candidates[i]
24+
now.append(n)
25+
find(i, score+n)
26+
now.pop()
27+
find(0, 0)
28+
return s
29+
30+

0 commit comments

Comments
ย (0)