-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathbemelon.py
25 lines (21 loc) ยท 1022 Bytes
/
bemelon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution:
# Space complexity: O(n)
# - n: len(candidates)
# - Stack Frame -> O(n)
# - list_of_combination -> O(n) ?
# Time complexity: O(n!)
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
list_of_combination = []
n = len(candidates)
def backtracking(curr: int, curr_combination: List[int], curr_sum: int):
if curr_sum == target: # ๋ชฉํ๊ฐ์ ๋๋ฌํ์ ๊ฒฝ์ฐ
list_of_combination.append(list(curr_combination))
return
if curr_sum > target: # ๋ชฉํ๊ฐ์ ์ด๊ณผํ ๊ฒฝ์ฐ
return
for i in range(curr, n):
curr_combination.append(candidates[i])
backtracking(i, curr_combination, curr_sum + candidates[i])
curr_combination.pop() # ๋ฐฑํธ๋ํน ๊ณผ์ ์์ ๋ง์ง๋ง ์์ ์ ๊ฑฐ
backtracking(0, [], 0)
return list_of_combination