-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.py
35 lines (33 loc) · 1012 Bytes
/
Solution.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
class Solution:
def numBusesToDestination(
self, routes: List[List[int]], source: int, target: int
) -> int:
if source == target:
return 0
s = [set(r) for r in routes]
d = defaultdict(list)
for i, r in enumerate(routes):
for v in r:
d[v].append(i)
g = defaultdict(list)
for ids in d.values():
m = len(ids)
for i in range(m):
for j in range(i + 1, m):
a, b = ids[i], ids[j]
g[a].append(b)
g[b].append(a)
q = deque(d[source])
ans = 1
vis = set(d[source])
while q:
for _ in range(len(q)):
i = q.popleft()
if target in s[i]:
return ans
for j in g[i]:
if j not in vis:
vis.add(j)
q.append(j)
ans += 1
return -1