LeetCode 947 Most Stones Removed with Same Row or Column - Solution

The core idea

Stones sharing a row or column form a connected component. Within each component, you can remove all stones except one (the "anchor").

Use Union-Find: if two stones share a row or column, union them. The answer is total stones minus the number of connected components.

Key insight: maximum removals = n - components. One stone per component must remain.