Minimum cable replug operations
There are n computers (0..n-1) connected by some bidirectional cables given as [a, b]. You may unplug any cable already in place and replug it between any two computers, including a pair not currently directly connected. Return the minimum number of such re-plug operations needed to make every computer connected to every other (one network). If it is impossible because there are not enough cables to span all computers, return -1. Duplicate cables and self-cables (a == b) may appear in the input.
Implement
min_reconnects(n: int, cables: list[list[int]]) → intExamples
in
[4,[[0,1],[0,2],[1,2]]]out1What a strong answer looks like
State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
0:00 of about 30 min
solution.py
InputExpectedGot
[4,[[0,1],[0,2],[1,2]]]1not run yetsample