Trains the technique from
LeetCode 847Shortest Path Visiting All NodesThis 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 network has hubs numbered 0 through n - 1, and links[i] lists the hubs joined directly to hub i. Every link works both ways, no hub is joined to itself, and every hub can be reached from every other one.
A courier may set off from whichever hub it likes, travels one link at a time, and may pass through any hub as often as it pleases.
Return the fewest links the courier has to cross to have called at every hub.
Example 1
The hubs form a single chain, so setting off at one end and walking to the other calls at all four hubs across three links.
Example 2
Four hubs hang off one middle hub. Setting off from a hanging hub, the courier has to come back through the middle between each pair of them, which is six links.
Example 3
A single hub is already called at, so nothing has to be crossed.
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 shortest_path_length(links: list[list[int]]) -> int:public int shortestPathLength(int[][] links)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.