Trains the technique from
LeetCode 815Bus RoutesThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A hospital campus is served by a fixed set of shuttle loops. Loop i calls at the docks listed in loops[i], cycling through them again and again without ever stopping.
A courier standing at a dock may board any loop that calls at that dock, stay aboard as long as they like, and step off at any other dock the loop calls at. Moving from one loop to another is only possible at a dock the two loops have in common, and the courier is not allowed to walk between docks.
The courier begins at dock start and needs to reach dock finish. Return the fewest loops the courier must board. When the courier is already standing at finish, the answer is 0. When no chain of loops joins the two docks, return -1.
Within a single loop every dock number is listed once, but two loops may well share docks. Dock numbers are only labels, so a number that appears in no loop simply has no shuttle service.
Example 1
Board the first loop at dock 4 and ride to dock 12, which both loops call at, then board the second loop there and ride to dock 5.
Example 2
The two loops share no dock, so a courier who boards at dock 3 can never reach dock 14.
Example 3
The courier is already where they need to be, so no shuttle is boarded at all.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def fewest_loops(loops: list[list[int]], start: int, finish: int) -> int:public int fewestLoops(int[][] loops, int start, int finish)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.