We have a city with bus stops and bus routes. Each bus route is a loop that visits a specific set of stops. We start at a particular stop and want to reach another stop using as few buses as possible. The answer counts buses boarded, not stops passed through.
This is a shortest path problem where the cost is per bus, not per stop. Riding one bus for 20 stops costs 1, while boarding a second bus to travel a single stop raises the total to 2.
Once you board a bus, every stop on its route is reachable at no extra cost. The question then becomes: what is the minimum number of routes we need to chain together to get from source to target? Two routes can be chained if they share at least one stop, because that shared stop is where we transfer from one bus to the other.
1 <= routes.length <= 500 -> The number of routes is small. A BFS whose nodes are whole routes searches at most 500 nodes.sum(routes[i].length) <= 10^5 -> The total number of stop entries across all routes is at most 100,000. This bounds the work of any approach that scans route contents.0 <= routes[i][j] < 10^6 -> Stop IDs can be up to a million, so we map stops to routes with a hash map rather than an array indexed by stop ID.Treat each bus stop as a node and run a BFS from source to target. Two stops are connected if some bus route contains both, and riding that bus from one stop to the other costs 1 bus. A single BFS step should therefore expand from a stop to every stop on every route that serves it.
For each stop we dequeue, find all routes that serve it, then add every unvisited stop on those routes to the queue. Each BFS level corresponds to boarding one more bus.
One hazard remains. A route can hold up to 100,000 stops, and the same route can be reached from many different stops, so re-expanding it on every encounter repeats that work. Tracking visited routes ensures each route's stops are expanded once, which keeps the total expansion work bounded by the total number of stop entries.
This approach is correct and fast, but the queue holds individual stops (up to 100,000 entries) while the answer counts buses. The next approach makes routes the nodes of the BFS, so the search operates directly on the quantity being minimized.
Run the BFS over routes instead of stops. Every route that contains the source stop is reachable with 1 bus. If any of those routes contains the target, the answer is 1. Otherwise, every route that shares a stop with one of them is reachable with 2 buses, because the shared stop is a transfer point. Expanding level by level finds the first route that contains the target.
This is a standard BFS where the nodes are route indices and two routes are neighbors if they share at least one stop. The number of buses equals the BFS level at which a route containing the target first appears. The queue now holds route indices (at most 500) instead of stop IDs (up to a million), and one BFS level corresponds to one bus.
BFS on an unweighted graph reaches every node at its minimum distance first, so the first route found to contain the target gives the minimum bus count. This is also why marking a route visited at enqueue time (rather than at dequeue time) is safe: any later path to that route uses at least as many buses. Enqueue-time marking also keeps each route in the queue at most once, so the queue never holds more than N entries.