-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path00040-combination_sum_II.py
42 lines (29 loc) · 1001 Bytes
/
00040-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
# 40: Combination Sum Ii
# https://leetcode.com/problems/combination-sum-ii
from typing import List
class Solution:
# SOLUTION
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
candidates.sort()
result = []
current = []
def backtrack(target: int, sum: int, start: int) -> None:
if start > target : return
if sum == target:
result.append(current.copy())
return
for i in range(start, len(candidates)):
if i>start and candidates[i] == candidates[i-1]: continue
current.append(candidates[i])
backtrack(target, sum + candidates[i], i+1)
current.pop()
backtrack(target, 0, 0)
return result
if __name__ == "__main__":
o = Solution()
# INPUT
candidates = [2,5,2,1,2]
target = 5
# OUTPUT
result = o.combinationSum2(candidates, target)
print(result)