Trains the technique from
LeetCode 2603Collect Coins in a TreeThis 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 of hubs is given as a tree: edges lists the links, and coins[i] is 1 when hub i holds a parcel and 0 otherwise.
A van starts at any hub it likes and must finish back where it started. A parcel is swept when the van visits some hub no more than two links away from the parcel's hub.
Every link travelled counts, and travelling the same link again counts again. Return the fewest links the round trip can travel while sweeping every parcel.
Example 1
The hubs run in a line of four with parcels at the two ends. Standing at either middle hub sweeps both ends, since each is within two links, so the van need not move at all.
Example 2
The line is eight hubs long with parcels at the ends. The van must reach within two links of each end, which means covering the two middle links and walking them both ways.
Example 3
A single hub with no parcel means there is nothing to sweep.
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 collect_the_coins(coins: list[int], edges: list[list[int]]) -> int:public int collectTheCoins(int[] coins, int[][] edges)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.