Skip to content

Commit 1d2f957

Browse files
authored
Create minimum-score-of-a-path-between-two-cities.py
1 parent 9aaa4b4 commit 1d2f957

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Time: O(n + m), m = len(roads)
2+
# Space: O(n + m)
3+
4+
# bfs
5+
class Solution(object):
6+
def minScore(self, n, roads):
7+
"""
8+
:type n: int
9+
:type roads: List[List[int]]
10+
:rtype: int
11+
"""
12+
def bfs():
13+
lookup = [False]*len(adj)
14+
q = [0]
15+
lookup[0] = True
16+
while q:
17+
new_q = []
18+
for u in q:
19+
for v, _ in adj[u]:
20+
if lookup[v]:
21+
continue
22+
lookup[v] = True
23+
new_q.append(v)
24+
q = new_q
25+
return lookup
26+
27+
adj = [[] for _ in xrange(n)]
28+
for u, v, w in roads:
29+
adj[u-1].append((v-1, w))
30+
adj[v-1].append((u-1, w))
31+
lookup = bfs()
32+
return min(w for u, _, w in roads if lookup[u-1])

0 commit comments

Comments
 (0)