Number of Operations to Make Network Connected
MediumLeetCodeUnion-Find (DSU)
Approach hintTry it first, then revealHide
Connecting n computers needs at least n - 1 cables, so return -1 when there are fewer. Otherwise every redundant cable can be moved to join two components; count the components with a DSU.