-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbusRoutes.py
More file actions
32 lines (29 loc) · 999 Bytes
/
busRoutes.py
File metadata and controls
32 lines (29 loc) · 999 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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/bus-routes/
# Author: Miao Zhang
# Date: 2021-03-13
class Solution:
def numBusesToDestination(self, routes: List[List[int]], source: int, target: int) -> int:
if source == target: return 0
# bus stop: ith bus travel
graph = collections.defaultdict(list)
for i in range(len(routes)):
for stop in routes[i]:
graph[stop].append(i)
visited = [0] * len(routes)
q = deque()
q.append(source)
res = 0
while q:
n = len(q)
res += 1
for _ in range(n):
stop = q.popleft()
for travel in graph[stop]:
if visited[travel]: continue
visited[travel] = 1
for s in routes[travel]:
if s == target: return res
q.append(s)
return -1