Most Stones Removed with Same Row or Column
Pattern: Disjoint set union
A stone can be removed if another stone shares its row or column. Stones linked through shared rows and columns form groups, and each group can be removed down to a single stone. So the answer is the number of stones minus the number of groups; union each stone's row with its column.
The problem
Stones sit at points [row, col], at most one per point. A stone may be removed if another stone remains in its row or
column. Return the most stones that can be removed.
Think first
- Which stones affect each other?
- How many stones of a group must stay?
- What can be the union-find elements?
Hints
Brute force, and why it fails
Union every pair of stones that share a row or column: O(n²).
The key insight
Treating rows and columns as the elements means each stone is a single union, so the pass is almost linear.
Dry run
Stones (0,0), (0,1), (1,0), (1,2), (2,1), (2,2). Row 0 joins (0,0) and (0,1); column 0 joins (0,0) and (1,0), and so on.
The plan
for (r, c): union(r, -c - 1), adding new elements as groups
return stones - groups
Complexity
- Time
O(n α(n))- Space
O(n)
One union per stone.
Edge cases
- One stone: 0.
- No two stones share a line: 0.
- All in one row: n - 1.
I've built my logic, show the code
Java
import java.util.*;
class Solution {
public int removeStones(int[][] stones) {
Map<Integer, Integer> parent = new HashMap<>(); // rows are 0.., columns are shifted to -1, -2, ...
int groups = 0;
for (int[] s : stones) {
int row = s[0], col = -s[1] - 1; // a stone joins its row and its column
for (int x : new int[]{row, col}) if (!parent.containsKey(x)) { parent.put(x, x); groups++; }
int a = find(parent, row), b = find(parent, col);
if (a != b) { parent.put(a, b); groups--; }
}
return stones.length - groups; // each group can be removed down to one stone
}
private int find(Map<Integer, Integer> parent, int x) {
while (parent.get(x) != x) { parent.put(x, parent.get(parent.get(x))); x = parent.get(x); }
return x;
}
}C++
class Solution {
public:
unordered_map<int, int> parent;
int removeStones(vector<vector<int>>& stones) {
parent.clear(); // rows are 0.., columns are shifted to -1, -2, ...
int groups = 0;
for (const vector<int>& s : stones) {
int row = s[0], col = -s[1] - 1; // a stone joins its row and its column
for (int x : {row, col}) if (!parent.count(x)) { parent[x] = x; groups++; }
int a = find(row), b = find(col);
if (a != b) { parent[a] = b; groups--; }
}
return static_cast<int>(stones.size()) - groups; // each group can be removed down to one stone
}
int find(int x) {
while (parent[x] != x) { parent[x] = parent[parent[x]]; x = parent[x]; }
return x;
}
};Python
class Solution:
def removeStones(self, stones: list[list[int]]) -> int:
parent = {} # rows are 0.., columns are shifted to -1, -2, ...
def find(x):
while parent[x] != x:
parent[x] = parent[parent[x]]
x = parent[x]
return x
groups = 0
for r, c in stones:
col = -c - 1 # a stone joins its row and its column
for x in (r, col):
if x not in parent:
parent[x] = x
groups += 1
a, b = find(r), find(col)
if a != b:
parent[a] = b
groups -= 1
return len(stones) - groups # each group can be removed down to one stoneCommon mistakes
- Using the same numbers for rows and columns.
- Counting groups of rows instead of groups that contain stones.