-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy path815.py
20 lines (20 loc) · 781 Bytes
/
815.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution:
def numBusesToDestination(self, routes, starterBus, targetBus):
path, travel, travelTaken, used = collections.defaultdict(set), [starterBus], 0, set()
for i, route in enumerate(routes):
for bus in route:
path[bus].add(i)
while travel:
new = []
for bus in travel:
if bus == targetBus:
return travelTaken
for route in path[bus]:
if route not in used:
used.add(route)
for nextBus in routes[route]:
if nextBus != bus:
new.append(nextBus)
travelTaken += 1
travel = new
return -1