Skip to content

Commit e0cc5c8

Browse files
committed
Combination Sum, Combination Sum II
1 parent 53abbd2 commit e0cc5c8

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

038 Combination Sum II py3.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/python3
2+
"""
3+
Given a collection of candidate numbers (candidates) and a target number
4+
(target), find all unique combinations in candidates where the candidate numbers
5+
sums to target.
6+
7+
Each number in candidates may only be used once in the combination.
8+
9+
Note:
10+
11+
All numbers (including target) will be positive integers.
12+
The solution set must not contain duplicate combinations.
13+
Example 1:
14+
15+
Input: candidates = [10,1,2,7,6,1,5], target = 8,
16+
A solution set is:
17+
[
18+
[1, 7],
19+
[1, 2, 5],
20+
[2, 6],
21+
[1, 1, 6]
22+
]
23+
Example 2:
24+
25+
Input: candidates = [2,5,2,1,2], target = 5,
26+
A solution set is:
27+
[
28+
[1,2,2],
29+
[5]
30+
]
31+
"""
32+
from typing import List
33+
34+
35+
class Solution:
36+
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
37+
ret = []
38+
candidates.sort()
39+
self.dfs(candidates, 0, [], 0, target, ret)
40+
return ret
41+
42+
def dfs(self, candidates, i, cur, cur_sum, target, ret):
43+
if cur_sum == target:
44+
ret.append(list(cur))
45+
return
46+
47+
if cur_sum > target or i >= len(candidates):
48+
return
49+
50+
# not choose A_i
51+
# to de-dup, need to jump
52+
j = i + 1
53+
while j < len(candidates) and candidates[j] == candidates[i]:
54+
j += 1
55+
56+
self.dfs(candidates, j, cur, cur_sum, target, ret)
57+
58+
# choose A_i
59+
cur.append(candidates[i])
60+
cur_sum += candidates[i]
61+
self.dfs(candidates, i + 1, cur, cur_sum, target, ret)
62+
cur.pop()
63+
cur_sum -= candidates[i]
64+
65+
66+
if __name__ == "__main__":
67+
assert Solution().combinationSum2([2,5,2,1,2], 5) == [[5], [1,2,2]]

039 Combination Sum py3.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/python3
2+
"""
3+
Given a set of candidate numbers (candidates) (without duplicates) and a target
4+
number (target), find all unique combinations in candidates where the candidate
5+
numbers sums to target.
6+
7+
The same repeated number may be chosen from candidates unlimited number of
8+
times.
9+
10+
Note:
11+
12+
All numbers (including target) will be positive integers.
13+
The solution set must not contain duplicate combinations.
14+
Example 1:
15+
16+
Input: candidates = [2,3,6,7], target = 7,
17+
A solution set is:
18+
[
19+
[7],
20+
[2,2,3]
21+
]
22+
Example 2:
23+
24+
Input: candidates = [2,3,5], target = 8,
25+
A solution set is:
26+
[
27+
[2,2,2,2],
28+
[2,3,3],
29+
[3,5]
30+
]
31+
"""
32+
from typing import List
33+
34+
35+
class Solution:
36+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
37+
ret = []
38+
self.dfs(candidates, 0, [], 0, target, ret)
39+
return ret
40+
41+
def dfs(self, candidates, i, cur, cur_sum, target, ret):
42+
if cur_sum == target:
43+
ret.append(list(cur))
44+
return
45+
46+
if cur_sum > target or i >= len(candidates):
47+
return
48+
49+
# not choose A_i
50+
self.dfs(candidates, i + 1, cur, cur_sum, target, ret)
51+
52+
# choose A_i
53+
cur.append(candidates[i])
54+
cur_sum += candidates[i]
55+
self.dfs(candidates, i, cur, cur_sum, target, ret)
56+
cur.pop()
57+
cur_sum -= candidates[i]

0 commit comments

Comments
 (0)