Trains the technique from
LeetCode 3600Maximize Spanning Tree Stability with UpgradesThis 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 mesh joins n relay sites numbered 0 through n - 1. Each entry edges[i] = [u, v, s, must] describes a possible two-way link between site u and site v whose signal strength is s. When must is 1 the link is already laid and has to stay; when it is 0 the link is optional. No two entries describe the same pair of sites.
The operator installs a set of links so that every site is reachable from every other and no loop is formed, which means exactly n - 1 links. Before doing so the operator may pick at most k of the optional links and double their signal strength. A link that has to stay can never be doubled.
The stability of the finished mesh is the strength of its weakest installed link. Return the largest stability achievable, or -1 if the links that have to stay already form a loop or no choice connects every site.
Example 1
Installing the links of strength 4 and 3 connects all three sites, and doubling the one of strength 3 makes it 6, so the weakest installed link has strength 4.
Example 2
All three links have to stay, and together they close a loop around the three sites, which the mesh is not allowed to contain.
Example 3
Site 2 appears in no link at all, so no set of links reaches every site and the answer is -1.
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_weakest_link(n: int, edges: list[list[int]], k: int) -> int:public int bestWeakestLink(int n, int[][] edges, int k)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.