-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCombination Sum II.py
46 lines (32 loc) · 1.67 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
# Link: https://leetcode.com/problems/combination-sum-ii/submissions/
from copy import deepcopy
class Solution:
def makeCombination(self, candidates, target, idx, currPossibleCombination, ans):
# If target is negative, exit function
if target < 0:
return
# If target equals to 0, make a copy of combination and add it to ans
if target == 0:
ans.append(deepcopy(currPossibleCombination))
return
# Iterate starting with next element
for i in range(idx, len(candidates)):
# Check if we're at the same position.
# Also check if current number doesn't match previous number
if i == idx or candidates[i] != candidates[i - 1]: # <= This prevents adding duplicates
# Add new element to list of possible combinations
currPossibleCombination.append(candidates[i])
# Make a recursive call with a new list, target, and index
self.makeCombination(candidates, target - candidates[i], i + 1, currPossibleCombination, ans)
# Remove recently added element and look for a new one
currPossibleCombination.pop(-1)
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
# If either variables are NoneType, exit function
if candidates == None or target == None:
return []
# Sort list
candidates.sort()
# Make all possible combinations
ans = list()
self.makeCombination(candidates, target, 0, [], ans)
return ans