forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
second-minimum-time-to-reach-destination.py
102 lines (94 loc) · 3.38 KB
/
second-minimum-time-to-reach-destination.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
102
# Time: O(|V| + |E|) = O(|E|) since graph is connected, O(|E|) >= O(|V|)
# Space: O(|V| + |E|) = O(|E|)
class Solution(object):
def secondMinimum(self, n, edges, time, change):
"""
:type n: int
:type edges: List[List[int]]
:type time: int
:type change: int
:rtype: int
"""
# Template:
# https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/find-if-path-exists-in-graph.py
def bi_bfs(adj, start, target):
left, right = {start}, {target}
lookup = set()
result = steps = 0
while left and (not result or result+2 > steps): # modified
for u in left:
lookup.add(u)
new_left = set()
for u in left:
if u in right:
if not result: # modified
result = steps
elif result < steps: # modifeid
return result+1
for v in adj[u]:
if v in lookup:
continue
new_left.add(v)
left = new_left
steps += 1
if len(left) > len(right):
left, right = right, left
return result+2 # modified
def calc_time(time, change, dist):
result = 0
for _ in xrange(dist):
if result//change%2:
result = (result//change+1)*change
result += time
return result
adj = [[] for _ in xrange(n)]
for u, v in edges:
adj[u-1].append(v-1)
adj[v-1].append(u-1)
return calc_time(time, change, bi_bfs(adj, 0, n-1))
# Time: O(|V| + |E|) = O(|E|) since graph is connected, O(|E|) >= O(|V|)
# Space: O(|V| + |E|) = O(|E|)
class Solution2(object):
def secondMinimum(self, n, edges, time, change):
"""
:type n: int
:type edges: List[List[int]]
:type time: int
:type change: int
:rtype: int
"""
INF = float("inf")
def bfs(adj, start):
q = [start]
dist = [INF]*len(adj)
dist[start] = 0
while q:
new_q = []
for u in q:
for v in adj[u]:
if dist[v] != INF:
continue
dist[v] = dist[u]+1
new_q.append(v)
q = new_q
return dist
def calc_time(time, change, dist):
result = 0
for _ in xrange(dist):
if result//change%2:
result = (result//change+1)*change
result += time
return result
adj = [[] for _ in xrange(n)]
for u, v in edges:
adj[u-1].append(v-1)
adj[v-1].append(u-1)
dist_to_end, dist_to_start = bfs(adj, 0), bfs(adj, n-1)
dist = dist_to_end[n-1]+2 # always exists
for i in xrange(n): # case of detour
if dist_to_end[i]+dist_to_start[i] == dist_to_end[n-1]:
continue
dist = min(dist, dist_to_end[i]+dist_to_start[i]) # find second min
if dist == dist_to_end[n-1]+1:
break
return calc_time(time, change, dist)