-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathOpen the Lock.py
37 lines (29 loc) · 933 Bytes
/
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
from collections import deque
from typing import List
class Solution:
"""
Time: O(n)
Memory: O(n)
"""
NEIGHBOURS = {
str(i): (str((i + 1) % 10), str((i - 1) % 10))
for i in range(10)
}
def openLock(self, deadends: List[str], target: str) -> int:
n = len(target)
start = '0' * n
if start in deadends:
return -1
queue = deque([(start, 0)])
seen = {start} | set(deadends)
while queue:
cur_node, depth = queue.popleft()
if cur_node == target:
return depth
for i in range(n):
for digit in self.NEIGHBOURS[cur_node[i]]:
new_node = cur_node[:i] + digit + cur_node[i + 1:]
if new_node not in seen:
seen.add(new_node)
queue.append((new_node, depth + 1))
return -1