Maximum Pebble Removals on a Coordinate Board
Trees & GraphsAuto-graded · 15 test casesLast updated
Compute how many pebbles can be taken off a board when each removal needs another remaining pebble in the same row or column, using connected components.
Pebble Removal Game
A board contains n pebbles, each on its own cell. The i-th pebble sits at the integer coordinate pebbles[i] = [row, col], and no two pebbles occupy the same cell.
You may repeatedly take a pebble off the board, but only under this rule: at the moment of removal, at least one other pebble still on the board must lie in the same row or in the same column as the pebble being removed.
Return the largest possible number of pebbles that can be removed by a sequence of legal moves.
Input
pebbles: a list ofnpairs[row, col]of non-negative integers (0-indexed coordinates).
Output
- A single integer: the maximum number of pebbles that can be removed.
Notes
- A pebble alone in both its row and its column (with respect to the pebbles currently on the board) cannot be removed.
- If there is only one pebble, the answer is
0.
Constraints
1 <= len(pebbles) <= 10000 <= row, col <= 10000- All coordinates in
pebblesare distinct.
Examples
Example 1
Input: max_pebble_removals([[0, 0], [0, 1], [1, 0], [1, 2], [2, 1], [2, 2]])
Output: 5
Example 2
Input: max_pebble_removals([[0, 0], [0, 2], [1, 1], [2, 0], [2, 2]])
Output: 3
Example 3
Input: max_pebble_removals([[0, 0]])
Output: 0
Hints
Hint 1
Think of each pebble as a node, and link two pebbles whenever they share a row or a column. What does a legal removal look like in this graph?
Hint 2
A pebble that is the last one left in its group of linked pebbles can never be removed. Conversely, show that a group of size k can always be reduced to a single pebble.
Hint 3
The answer is n minus the number of connected groups. To count groups in linear time, union each pebble with the first pebble seen in its row and the first seen in its column (or union row-nodes with column-nodes).
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Build a graph where each pebble is a vertex and two pebbles are adjacent if they share a row or a column. Each connected component of this graph can be reduced to exactly one pebble, and no further. So the answer is:
answer = n - (number of connected components)
Why it is correct
At least one pebble per component must stay. Consider the last remaining pebble of a component. Removing it would require another remaining pebble in its row or column, but any such pebble would belong to the same component, and none is left. So it cannot be removed.
A component of size k can lose k - 1 pebbles. Take a spanning tree of the component and root it anywhere. Remove pebbles in post-order (children before parents, i.e. reverse BFS order). When a pebble is removed, its parent in the tree is still on the board, and the tree edge guarantees they share a row or column. Every move is legal, and only the root remains.
Summing over components gives sum(k_i - 1) = n - components.
Counting components efficiently
Comparing all pairs would be O(n^2). Instead, use Union-Find and remember, for each row and each column, the index of the first pebble seen there. Every later pebble in that row (or column) is unioned with that first pebble. This yields the same connectivity as the full pairwise graph with at most 2n unions. Afterwards, the number of roots is the number of components.
Complexity
- Time: O(n * alpha(n)), effectively linear, using path halving and union by size.
- Space: O(n) for the DSU arrays and the two dictionaries.
Reference solution
from typing import List
def max_pebble_removals(pebbles: List[List[int]]) -> int:
n = len(pebbles)
parent = list(range(n))
size = [1] * n
def find(x):
while parent[x] != x:
parent[x] = parent[parent[x]]
x = parent[x]
return x
def union(a, b):
ra, rb = find(a), find(b)
if ra == rb:
return
if size[ra] < size[rb]:
ra, rb = rb, ra
parent[rb] = ra
size[ra] += size[rb]
row_owner = {}
col_owner = {}
for i, (row, col) in enumerate(pebbles):
if row in row_owner:
union(i, row_owner[row])
else:
row_owner[row] = i
if col in col_owner:
union(i, col_owner[col])
else:
col_owner[col] = i
components = sum(find(i) == i for i in range(n))
return n - components