Graph Theory37 sections · 1633 units
Open in Course

LeetCode 684 Redundant Connection - Logic

First Hit

Loop through the input edges. For every edge [u, v]:

1.1. Check find(u) and find(v).

2.2. If they are the same, this edge creates a cycle. Return it.

3.3. Otherwise, union(u, v) and continue.

The problem guarantees exactly one redundant edge, so you will always find it. Return the last edge that would form a cycle. Time: O(nα(n))O(n)O(n \cdot \alpha(n)) \approx O(n). Space: O(n)O(n) for the parent and rank arrays.