15% off all plans & roadmaps Use code
See plansLeetCode 947 · Solution & editorial
On a 2D plane, we place n stones at some integer coordinate points. Each coordinate point may have at most one stone.
Most Stones Removed with Same Row or Column is a Graph Traversal Patterns (DFS & BFS) problem, in the Union-Find (Disjoint Set Union - DSU) subpattern. The pattern covers DFS/BFS traversal, topological sort, shortest paths, Union-Find, and MST
Master Union-Find data structure with path compression and union by rank for connectivity problems.
The reference solution runs in O(n) time and O(n) space.
Most Stones Removed with Same Row or Column has been reported in interviews at Google.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Most Stones Removed with Same Row or Column is a Graph Traversal Patterns (DFS & BFS) problem, in the Union-Find (Disjoint Set Union - DSU) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.