Connected components after merges
You start with n isolated nodes labeled 0..n-1 and process a list of undirected merge operations [u, v]. After each operation, report the current number of connected components. Return a list of length len(ops) giving the component count after each merge in order. Merging two already-connected nodes leaves the count unchanged.
Implement
components_after_merges(n: int, ops: list[list[int]]) → list[int]Examples
in
[5,[[0,1],[2,3],[1,3],[0,2]]]out[4,3,2,2]What 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 20 min
solution.py
InputExpectedGot
[5,[[0,1],[2,3],[1,3],[0,2]]][4,3,2,2]not run yetsample