-
Notifications
You must be signed in to change notification settings - Fork 0
/
combination_sum_ii.py
54 lines (41 loc) · 1.74 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
# https://leetcode.com/problems/combination-sum-ii/description/
# git add . && git commit -m "completed combination_sum_ii" && git push && exit
from collections import defaultdict
from functools import cache
from typing import List
class Solution:
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
candidates_count = defaultdict(int)
for x in candidates:
candidates_count[x] += 1
candidates = sorted([x for x in candidates_count])
print(candidates)
current_state = []
current_sum = 0
current_step = 0
@cache
def helper(index,until_target):
nonlocal current_sum
print(current_state,index,until_target)
if until_target == 0:
return [current_state.copy(),]
if index == len(candidates) or candidates[index] > until_target:
return []
result = []
for _ in range(candidates_count[candidates[index]]):
current_state.append(candidates[index])
current_sum += candidates[index]
if current_sum > target:
break
print(current_state,index + 1,target - current_sum)
sub_result = helper(index + 1,target - current_sum)
for x in sub_result:
result.append(x)
while len(current_state) > 0 and current_state[-1] == candidates[index]:
current_state.pop()
current_sum -= candidates[index]
sub_result = helper(index + 1,until_target)
for x in sub_result:
result.append(x)
return result
return helper(0,target)