-
Notifications
You must be signed in to change notification settings - Fork 0
/
combination_sum.py
29 lines (22 loc) · 954 Bytes
/
combination_sum.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
26
27
28
29
# https://leetcode.com/problems/combination-sum/description/
# git add . && git commit -m "completed combination_sum" && git push && exit
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
def helper(el_count, i, target):
if el_count > 150:
return []
if i == len(candidates):
return []
sub_result = []
if target - candidates[i] == 0:
sub_result.append([candidates[i]])
if target - candidates[i] > 0:
res = helper(el_count + 1, i, target - candidates[i])
for r in res:
sub_result.append([candidates[i]] + r)
res = helper(el_count, i + 1, target)
for r in res:
sub_result.append(r)
return sub_result
candidates = list(set(candidates))
return helper(0, 0, target)