-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbacktracking.py
212 lines (152 loc) · 4.37 KB
/
backtracking.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
from math import inf
from typing import List
class Node:
def __init__(self, val, children=None):
self.val = val
self.children = [] if not children else children
def ternary_tree_paths(root):
res = []
def dfs(root, path):
if not root.children:
res.append("->".join(path) + "->" + str(root.val))
return
for child in root.children:
path.append(str(root.val))
dfs(child, path)
path.pop()
if root:
dfs(root, [])
return res
def letter_combination(n: int, letters: str) -> List[str]:
res = []
def dfs(path):
if len(path) == n:
res.append("".join(path))
return
for letter in letters:
path.append(letter)
dfs(path)
path.pop()
dfs([])
return res
number_to_letters = {
"2": "abc",
"3": "def",
"4": "ghi",
"5": "jkl",
"6": "mno",
"7": "pqrs",
"8": "tuv",
"9": "wxyz",
}
def letter_combinations_of_phone_number(digits: str) -> List[str]:
res = []
def dfs(start_index, path):
if len(path) == len(digits):
res.append("".join(path))
return
number = digits[start_index]
for letter in number_to_letters[number]:
path.append(letter)
dfs(start_index + 1, path)
path.pop()
dfs(0, [])
return res
def partition(s: str) -> List[List[str]]:
ans = []
n = len(s)
def is_palindrome(word):
return word == word[::-1]
def dfs(start, cur_path):
if start == n:
ans.append(cur_path[:])
return
for end in range(start + 1, n + 1):
prefix = s[start:end]
if is_palindrome(prefix):
dfs(end, cur_path + [prefix])
dfs(0, [])
return ans
def permutations(letters: str) -> List[str]:
res = []
def dfs(path, seen=set()):
if len(path) == len(letters):
res.append("".join(path))
return
for letter in letters:
if letter not in seen:
path.append(letter)
seen.add(letter)
dfs(path, seen)
path.pop()
seen.discard(letter)
dfs([])
return res
def word_break(s: str, words: List[str]) -> bool:
memo = {} # type: ignore
def dfs(start_index: int) -> bool:
if start_index == len(s):
return True
if start_index in memo:
return memo[start_index] # type: ignore
ans = False
for word in words:
if s[start_index:].startswith(word):
if dfs(start_index + len(word)):
ans = True
break
memo[start_index] = ans
return ans
return dfs(0)
def decode_ways(digits: str) -> int:
if len(digits) == 0:
return 1
elif digits[0] == "0":
return 0
else:
answer = decode_ways(digits[1:])
if 10 <= int(digits[:2]) <= 26:
answer += decode_ways(digits[2:])
return answer
def min_coins(coins, amount, sum):
if sum == amount:
return 0
if sum > amount:
return inf
ans = inf
for coin in coins:
result = min_coins(coins, amount, sum + coin)
if result == inf:
continue
ans = min(ans, result + 1)
return ans
def coin_change(coins: List[int], amount: int) -> int:
result = min_coins(coins, amount, 0)
return result if result != inf else -1 # type: ignore
def subsets(nums: List[int]) -> List[List[int]]:
res = []
def dfs(index, path):
res.append(path[:])
for i in range(index, len(nums)):
path.append(nums[i])
dfs(i + 1, path)
path.pop()
dfs(0, [])
return res
if __name__ == "__main__":
# left = Node(2)
# left.children.append(Node(3))
# middle = Node(4)
# right = Node(6)
# ternary_tree = Node(1)
# ternary_tree.children.append(left)
# ternary_tree.children.append(middle)
# ternary_tree.children.append(right)
# print(ternary_tree_paths(ternary_tree))
# print(letter_combination(0, "ab"))
# print(letter_combinations_of_phone_number("56"))
# print(permutations("abc"))
# print(decode_ways("102"))
# print(coin_change([1, 2, 5], 11))
# print(subsets([1, 2, 3]))
pass