Approach Summary
Union stones sharing the same row or column. Stones in the same component can all be removed except one. Answer = n - (number of components).
How to Recognize This Pattern
- "Remove stones sharing row or column"
- Union-Find on (row, col) — each group keeps one stone
Complexity Analysis
Time Complexity
O(n α(n))
Space Complexity
O(n)
Tags
Hash Table DFS Union-Find Graph