Problem Description
You are given an array routes representing bus routes where routes[i] is a bus route that the ith bus repeats forever. Starting at a bus stop source, you must determine the least number of buses you need to take to reach the bus stop target. You can only travel by following the bus routes. If it is not possible to reach the target, return -1.
Key Insights
- Model the problem as a graph where nodes are bus routes.
- Two bus routes are connected if they share at least one common bus stop.
- Use a hash map to quickly look up bus routes available at any bus stop.
- Employ Breadth-First Search (BFS) on the bus routes graph to find the minimum number of bus transfers.
- Mark visited bus routes and bus stops to avoid redundant work.
Space and Time Complexity
Time Complexity: O(N * L) in the worst-case scenario, where N is the number of bus routes and L is the average number of stops per route. Space Complexity: O(N * L) due to storage of the stop-to-buses mapping and BFS auxiliary data structures.
Solution
We begin by constructing a mapping from each bus stop to the list of buses passing through it. This allows quick retrieval of buses available at a given stop. Starting at the source stop, initialize the BFS queue with all buses that can be boarded at that stop. Then, for each bus route in the queue, inspect each stop along that route. If a stop is the target, return the number of buses taken so far. Otherwise, for each unvisited stop, add all adjacent bus routes (buses that pass through this stop) to the BFS queue with an incremented bus count. Continue until the queue is empty, which means the target cannot be reached.