-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path752.open-the-lock.py
59 lines (54 loc) · 1.76 KB
/
752.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
#
# @lc app=leetcode id=752 lang=python3
#
# [752] Open the Lock
#
# @lc code=start
# TAGS: BFS
# REVIEWME: BFS
class Solution:
# Time and Space: O(N^2*D^N), N is number of digits (4). D is 0 to 9 (10)
# Complexity break down:
# D^N combinations
# For each combination, there are N ways to turn, for each way, string slicing takes O(N)
# 704 ms, 43%. 1 directional bfs
def openLock(self, deadends: List[str], target: str) -> int:
visited = set(deadends)
q = [("0000", 0)]
for seq, depth in q:
if seq in visited:
continue
visited.add(seq)
if seq == target:
return depth
for i, c in enumerate(seq):
q.append(
(seq[:i] + f"{(int(c) + 1) % 10}" + seq[i + 1:], depth + 1))
q.append(
(seq[:i] + f"{(int(c) - 1) % 10}" + seq[i + 1:], depth + 1))
return -1
# 288 ms, 95%. 2 directional bfs
def openLock(self, deadends: List[str], target: str) -> int:
deadends = set(deadends)
heads = set(["0000"])
tails = set([target])
steps = 0
q = []
while heads or tails:
temp = set()
for seq in heads:
if seq in tails:
return steps
if seq in deadends:
continue
deadends.add(seq)
for i, c in enumerate(seq):
s1 = seq[:i] + f"{(int(c) + 1) % 10}" + seq[i + 1:]
s2 = seq[:i] + f"{(int(c) - 1) % 10}" + seq[i + 1:]
temp.add(s1)
temp.add(s2)
steps += 1
heads = tails
tails = temp
return -1
# @lc code=end