-
Notifications
You must be signed in to change notification settings - Fork 476
/
08. Open the Lock.py
33 lines (26 loc) Β· 1013 Bytes
/
08. 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
# https://leetcode.com/problems/open-the-lock/
# https://youtu.be/Pzg3bCDY87w
class Solution:
def openLock(self, deadends: List[str], target: str) -> int:
deadends = set(deadends)
if '0000' in deadends:
return -1
def children(lock):
arr = []
for i in range(4):
cur = int(lock[i])
arr.append(lock[:i] + str((cur+1)%10) + lock[i+1:])
arr.append(lock[:i] + str((cur-1)%10) + lock[i+1:])
return arr
visited = set()
q = collections.deque()
q.append(['0000', 0])
while q:
lock, turns = q.popleft()
if lock == target:
return turns
for child in children(lock):
if child not in deadends and child not in visited:
visited.add(child)
q.append([child, turns+1])
return -1