Trains the technique from
LeetCode 3620Network Recovery PathwaysThis 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 carrier is bringing a backbone back up. Stations are numbered from 0 to n - 1, where n is the length of online, and online[i] says whether station i is powered up. Traffic may pass through a station only when that station is powered up, and that includes the two ends of the run.
links[t] = [u, v, w] is a one-way fibre from station u to station v with u < v. The number w is that fibre's grade, and splicing better fibre costs more, so restoring that link also draws w credits. Two stations may be joined by more than one fibre.
A run is a sequence of links leading from station 0 to station n - 1. Restoring a run draws the total of the grades of its links, and the carrier has budget credits to spend, so a run is affordable when that total is at most budget. The rating of a run is the smallest grade among its links, because the run is only as good as its worst fibre.
Return the largest rating over all affordable runs whose stations are all powered up. Every grade is at least 1, so return -1 when no such run exists.
Example 1
The run through station 2 restores two links of grade 3 for 6 credits and rates 3, which is within the budget of 10 credits.
Example 2
The run through station 1 restores two links of grade 10 for 20 credits and rates 10, which is exactly the budget.
Example 3
The only run needs 6 credits and no credits are available, so nothing can be restored.
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_link_grade(links: list[list[int]], online: list[bool], budget: int) -> int:public int bestLinkGrade(int[][] links, boolean[] online, long budget)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.