Trains the technique from
LeetCode 797All Paths From Source to TargetThis 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 courier network runs n sorting hubs, numbered 0 through n - 1, where n is the length of links. The entry links[i] lists every hub that a parcel resting at hub i may be forwarded to. Forwarding works only in the direction listed, and the belts are laid out so that a parcel can never come back to a hub it has already left.
Parcels enter at hub 0 and leave from hub n - 1. A route is the run of hubs one parcel passes through on its way, written down in visiting order, with hub 0 first and hub n - 1 last. Two routes are different when their runs of hubs differ.
Return every route a parcel could take. The routes themselves may come back in any order, but the hubs inside each route must stay in visiting order. If nothing forwarded from hub 0 ever lands on hub n - 1, return an empty list.
Example 1
Hub 0 forwards to hub 2 and to hub 1, and each of the three answers listed runs from hub 0 to hub 5 using only forwardings named in `links`, for instance hub 0 to hub 1 to hub 4 to hub 5.
Example 2
Hub 1 forwards nowhere, so a parcel sent there stays put and that run of hubs is not a route. The single route ends on hub 4.
Example 3
Hub 0 forwards straight to hub 4, and it also forwards to hub 2, from where a parcel reaches hub 4 by way of hub 1.
The values you return may be in any order.
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 hub_routes(links: list[list[int]]) -> list[list[int]]:public List<List<Integer>> hubRoutes(int[][] links)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.