Bus Routes - Problem

You're planning a journey through a complex city bus system! 🚌

You have an array routes where each routes[i] represents the stops that bus i visits in order. Each bus follows its route in a circular pattern forever. For example, if routes[0] = [1, 5, 7], then bus 0 travels: 1 β†’ 5 β†’ 7 β†’ 1 β†’ 5 β†’ 7 β†’ ... infinitely.

You start at bus stop source (not on any bus initially) and want to reach bus stop target. You can only travel by taking buses - no walking between stops!

Goal: Find the minimum number of buses you need to take to get from source to target. Return -1 if it's impossible.

Think of this as finding the shortest path in a graph where nodes are bus stops and edges represent bus connections! 🎯

Input & Output

example_1.py β€” Basic Bus Transfer
$ Input: routes = [[1,2,7],[3,6,7]], source = 1, target = 6
β€Ί Output: 2
πŸ’‘ Note: Take bus 0 (route [1,2,7]) from stop 1 to stop 7, then take bus 1 (route [3,6,7]) from stop 7 to stop 6. Total: 2 buses.
example_2.py β€” No Transfer Needed
$ Input: routes = [[7,12],[4,5,15],[6],[15,19,26,7]], source = 15, target = 12
β€Ί Output: -1
πŸ’‘ Note: There's no way to get from stop 15 to stop 12. Stop 15 is on routes [4,5,15] and [15,19,26,7], but neither connects to any route containing stop 12.
example_3.py β€” Same Source and Target
$ Input: routes = [[1,2,7],[3,6,7]], source = 2, target = 2
β€Ί Output: 0
πŸ’‘ Note: Already at the target stop, no buses needed.

Visualization

Tap to expand
Route ARoute BRoute CRoute DRoute ESTBus Routes as Graph Nodesβ€’ Solid circles: Bus stopsβ€’ Dashed lines: Route connectionsβ€’ BFS finds minimum route hops
Understanding the Visualization
1
Build Route Graph
Map each stop to all buses serving it, creating connections between routes
2
BFS on Routes
Start BFS from all buses serving source, treating each route as one hop
3
Check Route Coverage
For each route, check if it contains target before expanding
4
Expand to Connected Routes
Add all buses reachable from any stop on current route
Key Takeaway
🎯 Key Insight: Transform the problem from finding shortest path between stops to finding shortest path between bus routes, dramatically reducing complexity from O(stopsΒ²) to O(routes Γ— avg_stops_per_route)

Time & Space Complexity

Time Complexity
⏱️
O(N Γ— M)

N is total number of stops across all routes, M is number of bus routes. Each stop is processed at most once per bus route

n
2n
βœ“ Linear Growth
Space Complexity
O(N + M)

Space for stop-to-buses mapping (N) and BFS queue/visited set (M)

n
2n
βœ“ Linear Space

Constraints

  • 1 ≀ routes.length ≀ 500
  • 1 ≀ routes[i].length ≀ 105
  • All values of routes[i] are unique
  • Sum of routes[i].length ≀ 105
  • 0 ≀ routes[i][j] ≀ 106
  • 0 ≀ source, target ≀ 106
Asked in
Google 42 Amazon 35 Meta 28 Microsoft 22 Apple 18 Uber 15
67.2K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen