-
Notifications
You must be signed in to change notification settings - Fork 0
/
40.combination-sum-ii.py
98 lines (80 loc) · 3.49 KB
/
40.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
#
# @lc app=leetcode id=40 lang=python3
#
# [40] Combination Sum II
#
# @lc code=start
from typing import List
class Solution:
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
"""
edge : next value of the candidate list
- include or not
- which means, it's binary tree, each vertex has exactly two child nodes
- to avoid duplicated calculation, only consider next indices value (like i did in my Solution)
vertex
- sum of values that added from the so far path
"""
candidates.sort()
result = []
def backtrack(cur: list, pos: int, target: int):
if target==0:
result.append(cur[:])
return
if target <=0: return
prev = -1
for i in range(pos, len(candidates)):
candidate = candidates[i]
if candidate <= prev:
print(candidate, prev)
continue
cur.append(candidate)
backtrack(cur, i+1, target-candidate)
cur.pop()
prev = candidate
backtrack([], 0, target)
return result
def combinationSum2_timeexceed(self, candidates: List[int], target: int) -> List[List[int]]:
"""time exceed
edge: value that is about to include
vertex: remained value to the target after include the value of the candidates"""
"""[1] stack result에 넣을 때 sort, 구분자 넣어서 string으로 만들고 result에 add"""
stack, result = [], set()
candidates.sort()
def backtrack(idx: int, remainder: int):
"""
:idx: 호출한 함수에서 계산에 포함한 candidate index
:remainder: 현재 target까지 남은 값
"""
if remainder == 0: # stack result에 넣어주기
result.add(' '.join(sorted(stack)))
return
if remainder<0: return
for i, candidate in enumerate(candidates[idx+1:]):
i += idx+1
if candidate > remainder: continue
stack.append(str(candidate))
backtrack(i, remainder-candidate)
stack.pop()
backtrack(-1, target)
# print(result)
return [list(map(int, r.split())) for r in result]
def combinationSum_dp(self, candidates: List[int], target: int) -> List[List[int]]:
"""
아까 풀었던 39번 문제에서 원소 중복으로 사용할 수 없는 조건만 더한 문제
"""
candidates.sort()
dp = [[] for _ in range(target+1)]
for i, c in enumerate(candidates):
if c>target: break # already bigger than target, no need to search for the combination sum
i+=1
# for j in range(i, len(candidates)):
for j in range(i, target+1):
if j == i: dp[i].append([c])
print(f"i={i}, c={c}, j={j}, cand[j]={candidates[j]}, dp={dp}")
for comb in dp[candidates[j]-c]:
dp[i].append(comb + [c])
Solution().combinationSum2([10,1,2,7,6,1,5],8)
# Solution().combinationSum_BT([2,5,2,1,2], 5)
# Solution().combinationSum2([1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], 30)
# @lc code=end