Skip to main content
Medium Union Find Medium frequency

Most Stones Removed with Same Row or Column

Open on LeetCode

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

This site is free. If these guides are helping your prep, consider buying me a coffee. ☕

Support →
Buy me a coffee