-
Notifications
You must be signed in to change notification settings - Fork 43
/
open-the-lock.py
101 lines (98 loc) · 3.79 KB
/
open-the-lock.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
# V0
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/82703064
class Solution:
def openLock(self, deadends, target):
"""
:type deadends: List[str]
:type target: str
:rtype: int
"""
deadset = set(deadends)
if (target in deadset) or ("0000" in deadset): return -1
que = collections.deque()
que.append("0000")
visited = set(["0000"])
step = 0
while que:
step += 1
size = len(que)
for i in range(size):
point = que.popleft()
for j in range(4):
for k in range(-1, 2, 2):
newPoint = [i for i in point]
newPoint[j] = chr((ord(newPoint[j]) - ord('0') + k + 10) % 10 + ord('0'))
newPoint = "".join(newPoint)
if newPoint == target:
return step
if (newPoint in deadset) or (newPoint in visited):
continue
que.append(newPoint)
visited.add(newPoint)
return -1
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/82703064
class Solution(object):
def openLock(self, deadends, target):
"""
:type deadends: List[str]
:type target: str
:rtype: int
"""
que = collections.deque()
que.append("0000")
visited = set(deadends)
step = 0
while que:
size = len(que)
for _ in range(size):
node = que.popleft()
if node in visited:
continue
visited.add(node)
if node == target:
return step
nodelist = map(int, list(node))
que.append("".join(map(str, [(nodelist[0] + 1 + 10) % 10, nodelist[1], nodelist[2], nodelist[3]])))
que.append("".join(map(str, [(nodelist[0] - 1 + 10) % 10, nodelist[1], nodelist[2], nodelist[3]])))
que.append("".join(map(str, [nodelist[0], (nodelist[1] + 1 + 10) % 10, nodelist[2], nodelist[3]])))
que.append("".join(map(str, [nodelist[0], (nodelist[1] - 1 + 10) % 10, nodelist[2], nodelist[3]])))
que.append("".join(map(str, [nodelist[0], nodelist[1], (nodelist[2] + 1 + 10) % 10, nodelist[3]])))
que.append("".join(map(str, [nodelist[0], nodelist[1], (nodelist[2] - 1 + 10) % 10, nodelist[3]])))
que.append("".join(map(str, [nodelist[0], nodelist[1], nodelist[2], (nodelist[3] + 1 + 10) % 10])))
que.append("".join(map(str, [nodelist[0], nodelist[1], nodelist[2], (nodelist[3] - 1 + 10) % 10])))
step += 1
return -1
# V2
# Time: O(k * n^k + d), n is the number of alphabets,
# k is the length of target,
# d is the size of deadends
# Space: O(k * n^k + d)
class Solution(object):
def openLock(self, deadends, target):
"""
:type deadends: List[str]
:type target: str
:rtype: int
"""
dead = set(deadends)
q = ["0000"]
lookup = {"0000"}
depth = 0
while q:
next_q = []
for node in q:
if node == target: return depth
if node in dead: continue
for i in range(4):
n = int(node[i])
for d in (-1, 1):
nn = (n+d) % 10
neighbor = node[:i] + str(nn) + node[i+1:]
if neighbor not in lookup:
lookup.add(neighbor)
next_q.append(neighbor)
q = next_q
depth += 1
return -1