Most Stones Removed
Master this topic with zero to advance depth.
Most Stones Removed
On a 2D plane, return the largest number of stones that can be removed. A stone can be removed if it shares a row or column with another stone.
Examples
Input: stones = [[0,0],[0,1],[1,0],[1,2],[2,1],[2,2]]
Output: 5
Approach 1
Level I: DFS (Component Counting)
Intuition
Represent stones as nodes and connect them if they share a row/column. Total stones - Number of components = Max stones removed.
⏱ O(N^2)💾 O(N)
Detailed Dry Run
6 stones. DFS finds 1 connected component. 6 - 1 = 5.
Approach 2
Level III: Union-Find (Row-Column Mapping)
Intuition
Treat rows and columns as nodes. For each stone (r, c), union row r with column c. The number of stones removed is (Total Stones - Number of components).
⏱ O(N \cdot \alpha(N))💾 O(Range)
Detailed Dry Run
Stone (0, 0): Union row 0 with column 10001 (offset to distinguish).
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.