forked from prabhupant/python-ds
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombination_sum.py
More file actions
30 lines (26 loc) · 912 Bytes
/
Copy pathcombination_sum.py
File metadata and controls
30 lines (26 loc) · 912 Bytes
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
30
"""
High Level Description:
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target),
find all unique combinations in candidates where the candidate numbers sums to target.
The same repeated number may be chosen from candidates unlimited number of times.
Note:
* All numbers (including target) will be positive integers.
* The solution set must not contain duplicate combinations.
"""
def combinationSum(candidates, target):
res = []
candidates.sort()
dfs(candidates, target, 0, [], res)
return res
def dfs(nums, target, index, path, res):
if target < 0:
return # backtracking
if target == 0:
res.append(path)
return
for i in range(index, len(nums)):
dfs(nums, target-nums[i], i, path+[nums[i]], res)
'''Usage Example'''
# candidates = [2,3,6,7]
# target = 7
# print(combinationSum(candidates, target))