Trains the technique from
LeetCode 310Minimum Height TreesThis 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 campus has n buildings numbered 0 through n - 1, joined by covered walkways. The walkways arrive as a list of pairs: walkways[k] = [a, b] means one walkway directly joins building a and building b. There are n - 1 walkways, every building is reachable from every other along them, and no walkway is listed twice, so the campus forms a tree.
An internal courier is to be based in one building. The reach of building h is the number of walkways on the route from h to the building furthest from it, counting walkways along the single route that joins the two.
Return every building whose reach is as small as possible, listed in ascending order. For a campus shaped like this there is always either one such building or two.
Example 1
From building 3 the routes reach buildings 0, 1, 2 and 4 across one walkway and buildings 5 and 6 across two, so its reach is 2. From building 4 the routes reach buildings 3, 5 and 6 across one walkway and buildings 0, 1 and 2 across two, so its reach is also 2. Both are reported, in ascending order.
Example 2
The campus is a single corridor. From building 2 the furthest buildings are 0 and 4, each two walkways away, so its reach is 2 and it is the only building reported.
Example 3
Each building is one walkway from the other, so both have reach 1 and both are reported.
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 best_courier_bases(n: int, walkways: list[list[int]]) -> list[int]:public List<Integer> bestCourierBases(int n, int[][] walkways)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.