Skip to content

Commit 1ac5131

Browse files
committed
feat: Solve combination-sum problem
1 parent 176ddb4 commit 1ac5131

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

โ€Žcombination-sum/hu6r1s.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
class Solution:
2+
"""
3+
์‹œ๊ฐ„ ๋ณต์žก๋„ (Time Complexity):
4+
- ์ด ๋ฌธ์ œ๋Š” ๋ฐฑํŠธ๋ž˜ํ‚น(DFS) ๊ธฐ๋ฐ˜์œผ๋กœ ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ์กฐํ•ฉ์„ ํƒ์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.
5+
- ์ตœ์•…์˜ ๊ฒฝ์šฐ ๊ฐ ์กฐํ•ฉ์—์„œ ํ•œ ์ˆซ์ž๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ํŠธ๋ฆฌ์˜ ๊นŠ์ด๋Š” ์ตœ๋Œ€ target // min(candidates)
6+
- ๊ฐ ๊นŠ์ด๋งˆ๋‹ค ์ตœ๋Œ€ len(candidates)๋งŒํผ ๋ถ„๊ธฐ ๊ฐ€๋Šฅ
7+
- ๋”ฐ๋ผ์„œ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” ์ง€์ˆ˜์ ์œผ๋กœ ์ฆ๊ฐ€: O(2^T), T = target
8+
(์ •ํ™•ํ•œ upper bound๋Š” ๊ณ„์‚ฐํ•˜๊ธฐ ์–ด๋ ต์ง€๋งŒ, ๋Œ€๋žต์ ์œผ๋กœ๋Š” O(2^T) ๋˜๋Š” O(k^T)๋กœ ๋ณผ ์ˆ˜ ์žˆ์Œ)
9+
10+
๊ณต๊ฐ„ ๋ณต์žก๋„ (Space Complexity):
11+
- ์žฌ๊ท€ ํ˜ธ์ถœ์˜ ์ตœ๋Œ€ ๊นŠ์ด: O(T), T = target (๊ฐ€์žฅ ์ž‘์€ ์ˆซ์ž๋งŒ ๋ฐ˜๋ณตํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ)
12+
- ๊ฒฝ๋กœ ์ €์žฅ์šฉ ๋ฆฌ์ŠคํŠธ(nums): O(T)
13+
- ๊ฒฐ๊ณผ ์ €์žฅ์šฉ ๋ฆฌ์ŠคํŠธ(output): ์ตœ์•…์˜ ๊ฒฝ์šฐ ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ์กฐํ•ฉ ์ €์žฅ โ†’ O(number of valid combinations * ํ‰๊ท  ๊ธธ์ด)
14+
์ตœ์ข… ๊ณต๊ฐ„ ๋ณต์žก๋„: **O(T + R)**,
15+
T: ์žฌ๊ท€ ๊นŠ์ด / R: ๊ฒฐ๊ณผ ์กฐํ•ฉ ์ˆ˜๊ฐ€ ํด ๊ฒฝ์šฐ output์ด ์ฐจ์ง€ํ•˜๋Š” ๊ณต๊ฐ„
16+
17+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
18+
output, nums = [], []
19+
20+
def dfs(start, total):
21+
if total > target:
22+
return
23+
if total == target:
24+
output.append(nums[:])
25+
26+
for i in range(start, len(candidates)):
27+
nums.append(candidates[i])
28+
dfs(i, total + candidates[i])
29+
nums.pop()
30+
31+
dfs(0, 0)
32+
return output
33+
"""
34+
"""
35+
2. dp
36+
dp[i]๋Š” ์ˆซ์ž๋“ค์„ ๋”ํ•ด์„œ ํ•ฉ์ด i๊ฐ€ ๋˜๋Š” ๋ชจ๋“  ์กฐํ•ฉ๋“ค์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
37+
dp[num - candidate]์— ์žˆ๋Š” ์กฐํ•ฉ์— candidate๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด num์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ํ™•์žฅํ•ฉ๋‹ˆ๋‹ค.
38+
์ค‘๋ณต๋œ ์กฐํ•ฉ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด candidates๋ฅผ ๋ฐ”๊นฅ ๋ฃจํ”„์— ๋‘  (์ฆ‰, ๊ฐ™์€ ์ˆซ์ž๋ฅผ ๊ณ„์† ์žฌ์‚ฌ์šฉํ•˜๋˜, ์ด์ „ ์ˆซ์ž๋ถ€ํ„ฐ ๋ˆ„์ ).
39+
40+
์‹œ๊ฐ„ ๋ณต์žก๋„ (Time Complexity):
41+
๋ฐ”๊นฅ ๋ฃจํ”„: ํ›„๋ณด ์ˆซ์ž๋งŒํผ โ†’ O(N)
42+
์•ˆ์ชฝ ๋ฃจํ”„: target๊นŒ์ง€ ๋ฐ˜๋ณต โ†’ O(T)
43+
๊ฐ€์žฅ ์•ˆ์ชฝ ๋ฃจํ”„: dp[num - candidate]์— ์žˆ๋Š” ์กฐํ•ฉ๋“ค์„ ๋ชจ๋‘ ์ˆœํšŒ โ†’ O(A) (A๋Š” ์กฐํ•ฉ ๊ฐœ์ˆ˜ ๋ฐ ๊ธธ์ด์— ๋น„๋ก€)
44+
โ†’ ๋”ฐ๋ผ์„œ ์ตœ์•…์˜ ๊ฒฝ์šฐ O(N ร— T ร— A)
45+
46+
๊ณต๊ฐ„ ๋ณต์žก๋„ (Space Complexity):
47+
dp ๋ฐฐ์—ด ํฌ๊ธฐ: target + 1 โ†’ O(T)
48+
๊ฐ dp[i]์— ์ €์žฅ๋œ ์กฐํ•ฉ ๋ฆฌ์ŠคํŠธ๋“ค์˜ ๊ฐœ์ˆ˜์™€ ๊ธธ์ด โ†’ O(A)
49+
๋”ฐ๋ผ์„œ ์ „์ฒด ๊ณต๊ฐ„์€ O(T ร— A)
50+
51+
Example 2์˜ dp ์ถœ๋ ฅ:
52+
53+
[[[]], [], [[2]], [], [], [], [], [], []]
54+
[[[]], [], [[2]], [], [[2, 2]], [], [], [], []]
55+
[[[]], [], [[2]], [], [[2, 2]], [], [[2, 2, 2]], [], []]
56+
[[[]], [], [[2]], [], [[2, 2]], [], [[2, 2, 2]], [], [[2, 2, 2, 2]]]
57+
[[[]], [], [[2]], [[3]], [[2, 2]], [], [[2, 2, 2]], [], [[2, 2, 2, 2]]]
58+
[[[]], [], [[2]], [[3]], [[2, 2]], [[2, 3]], [[2, 2, 2]], [], [[2, 2, 2, 2]]]
59+
[[[]], [], [[2]], [[3]], [[2, 2]], [[2, 3]], [[2, 2, 2], [3, 3]], [], [[2, 2, 2, 2]]]
60+
[[[]], [], [[2]], [[3]], [[2, 2]], [[2, 3]], [[2, 2, 2], [3, 3]], [[2, 2, 3]], [[2, 2, 2, 2]]]
61+
[[[]], [], [[2]], [[3]], [[2, 2]], [[2, 3]], [[2, 2, 2], [3, 3]], [[2, 2, 3]], [[2, 2, 2, 2], [2, 3, 3]]]
62+
[[[]], [], [[2]], [[3]], [[2, 2]], [[2, 3], [5]], [[2, 2, 2], [3, 3]], [[2, 2, 3]], [[2, 2, 2, 2], [2, 3, 3]]]
63+
[[[]], [], [[2]], [[3]], [[2, 2]], [[2, 3], [5]], [[2, 2, 2], [3, 3]], [[2, 2, 3], [2, 5]], [[2, 2, 2, 2], [2, 3, 3]]]
64+
[[[]], [], [[2]], [[3]], [[2, 2]], [[2, 3], [5]], [[2, 2, 2], [3, 3]], [[2, 2, 3], [2, 5]], [[2, 2, 2, 2], [2, 3, 3], [3, 5]]]
65+
"""
66+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
67+
dp = [[] for _ in range(target + 1)]
68+
dp[0] = [[]]
69+
70+
for candidate in candidates:
71+
for num in range(candidate, target + 1):
72+
for combination in dp[num - candidate]:
73+
dp[num].append(combination + [candidate])
74+
return dp[target]

0 commit comments

Comments
ย (0)