-
Notifications
You must be signed in to change notification settings - Fork 43
/
combination-sum-ii.py
234 lines (191 loc) · 6.75 KB
/
combination-sum-ii.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
"""
40. Combination Sum II
Medium
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target.
Each number in candidates may only be used once in the combination.
Note: The solution set must not contain duplicate combinations.
Example 1:
Input: candidates = [10,1,2,7,6,1,5], target = 8
Output:
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]
Example 2:
Input: candidates = [2,5,2,1,2], target = 5
Output:
[
[1,2,2],
[5]
]
Constraints:
1 <= candidates.length <= 100
1 <= candidates[i] <= 50
1 <= target <= 30
"""
# V0
class Solution(object):
def combinationSum2(self, candidates, target):
"""
NOTE : we have index here for tracking
-> avoid reuse same element from candidates
"""
def dfs(nums, target, index, res, path):
if target < 0:
return
elif target == 0:
res.append(path)
return
for i in range(index, len(nums)):
### NOTE : below condition
if i > index and nums[i] == nums[i-1]:
continue
dfs(nums, target - nums[i], i + 1, res, path + [nums[i]])
### NOTE : once we sort the input array, the occurrance of each unique number would be adjacent to each other.
candidates.sort()
print(candidates)
res = []
dfs(candidates, target, 0, res, [])
return res
# V0
# IDEA : backtrack
# TODO : fix TLE
# from collections import Counter
# class Solution(object):
# def combinationSum2(self, candidates, target):
# def help(idx, cur, visited):
# if sum(cur) == target:
# tmp = cur[:]
# tmp.sort()
# if tmp not in res:
# res.append(tmp)
# cur = []
# visited =[]
# return
# if sum(cur) > target or len(cur) > _len:
# cur = []
# visited = []
# return
# for i in range(idx, _len):
# if i not in visited:
# visited.append(i)
# cur.append(candidates[i])
# help(idx+1, cur, visited)
# cur.pop(-1)
# visited.pop(-1)
# # edge case
# if not candidates and target:
# return []
# if sum(candidates) < target:
# return []
# res = []
# cur = []
# visited = []
# idx = 0
# candidates.sort()
# _len = len(candidates)
# cnt = Counter(candidates)
# help(idx, cur, visited)
# print ("res = " + str(res))
# return res
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/79343638
# IDEA : DFS
class Solution(object):
def combinationSum2(self, candidates, target):
candidates.sort()
print(candidates)
res = []
self.dfs(candidates, target, 0, res, [])
return res
def dfs(self, nums, target, index, res, path):
if target < 0:
return
elif target == 0:
res.append(path)
return
for i in range(index, len(nums)):
if i > index and nums[i] == nums[i-1]:
continue
self.dfs(nums, target - nums[i], i + 1, res, path + [nums[i]])
# V1'
# IDEA : BACKTRACKING
# https://leetcode.com/problems/combination-sum-ii/solution/
class Solution:
def combinationSum2(self, candidates, target):
def backtrack(comb, remain, curr, counter, results):
if remain == 0:
# make a deep copy of the current combination
# rather than keeping the reference.
results.append(list(comb))
return
elif remain < 0:
return
for next_curr in range(curr, len(counter)):
candidate, freq = counter[next_curr]
if freq <= 0:
continue
# add a new element to the current combination
comb.append(candidate)
counter[next_curr] = (candidate, freq-1)
# continue the exploration with the updated combination
backtrack(comb, remain - candidate, next_curr, counter, results)
# backtrack the changes, so that we can try another candidate
counter[next_curr] = (candidate, freq)
comb.pop()
results = [] # container to hold the final combinations
counter = Counter(candidates)
# convert the counter table to a list of (num, count) tuples
counter = [(c, counter[c]) for c in counter]
backtrack(comb = [], remain = target, curr = 0,
counter = counter, results = results)
return results
# V1'
# IDEA : BACKTRACKING with index
# https://leetcode.com/problems/combination-sum-ii/solution/
class Solution:
def combinationSum2(self, candidates, target: int):
def backtrack(comb, remain, curr, results):
if remain == 0:
# make a deep copy of the resulted combination
results.append(list(comb))
return
for next_curr in range(curr, len(candidates)):
if next_curr > curr \
and candidates[next_curr] == candidates[next_curr-1]:
continue
pick = candidates[next_curr]
# optimization: skip the rest of elements starting from 'curr' index
if remain - pick < 0:
break
comb.append(pick)
backtrack(comb, remain - pick, next_curr + 1, results)
comb.pop()
candidates.sort()
comb, results = [], []
backtrack(comb, target, 0, results)
return results
# V2
# Time: O(k * C(n, k))
# Space: O(k)
class Solution(object):
# @param candidates, a list of integers
# @param target, integer
# @return a list of lists of integers
def combinationSum2(self, candidates, target):
result = []
self.combinationSumRecu(sorted(candidates), result, 0, [], target)
return result
def combinationSumRecu(self, candidates, result, start, intermediate, target):
if target == 0:
result.append(list(intermediate))
prev = 0
while start < len(candidates) and candidates[start] <= target:
if prev != candidates[start]:
intermediate.append(candidates[start])
self.combinationSumRecu(candidates, result, start + 1, intermediate, target - candidates[start])
intermediate.pop()
prev = candidates[start]
start += 1