My first instinct was Dijkstra and that was the wrong direction to go down.
Model the problem as a graph where each bus route is a node, and edges exist between routes that share at least one stop. Then perform BFS from all routes containing the start stop to find the minimum number of buses to reach any route containing the target stop. If start equals target, return 0; if no such route exists, return -1.
Pro tip: Clarify upfront that boarding a bus counts as one, so the answer is the number of buses boarded, not the number of transfers. Also, mention that if the start and target stops are the same, the answer is 0, which is a common edge case.
Confirm that the start and target stops are distinct and that each bus route is circular. If start equals target, return 0 immediately.
Create a hash map from each stop to the list of routes that include it. This allows quick lookup of which buses can be boarded at any stop.
For each pair of routes that share at least one stop, add an undirected edge. This graph represents which buses can be transferred between.
Initialize a queue with all routes containing the start stop, marking them visited with distance 1. Perform BFS, and when a route containing the target stop is dequeued, return its distance.
If BFS exhausts all reachable routes without finding the target, return -1. Otherwise, the BFS distance gives the minimum number of buses needed.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.