-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0752-open-the-lock.py
More file actions
35 lines (29 loc) · 969 Bytes
/
0752-open-the-lock.py
File metadata and controls
35 lines (29 loc) · 969 Bytes
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
# time complexity: O(n^2)
# space complexity: O(n)
from collections import deque
from typing import List
class Solution:
def openLock(self, deadends: List[str], target: str) -> int:
depth = 0
visited, q = set(deadends), deque(["0000"])
while q:
sz = len(q)
for _ in range(sz):
cur = q.popleft()
if cur == target:
return depth
if cur not in visited:
q.extend(self.getNeighbors(cur))
visited.add(cur)
depth += 1
return -1
def getNeighbors(self, s):
res = []
for i, c in enumerate(s):
n = int(c)
res.append(s[: i] + str((n - 1) % 10) + s[i + 1:])
res.append(s[: i] + str((n + 1) % 10) + s[i + 1:])
return res
deadends = ["0201", "0101", "0102", "1212", "2002"]
target = "0202"
print(Solution().openLock(deadends, target))