Trains the technique from
LeetCode 1319Number of Operations to Make Network ConnectedThis 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 rack holds n machines numbered 0 through n - 1. Each entry cables[i] = [a, b] is a cable running directly between machine a and machine b.
In one move you may unplug a cable at both ends and plug it back in between any two machines you choose.
Two machines can talk when some run of cables leads from one to the other. Return the fewest moves that leave every machine able to talk to every other, or -1 when no number of moves can manage it.
Example 1
Machines 0, 1 and 2 are cabled in a ring, so one of their three cables is spare. Machines 3 and 4 form a second group. Moving the spare cable to run between the two groups joins the whole rack in one move.
Example 2
Seven machines need at least six cables to hold together and only three are on the rack, so no amount of replugging can do it.
Example 3
The cables already run through every machine in one line, so nothing needs moving.
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 make_connected(n: int, cables: list[list[int]]) -> int:public int makeConnected(int n, int[][] cables)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.