2Brute2Best
🔥0

Most Stones Removed with Same Row or Column

Medium·LeetCode #947 Pro OnlyMedium
LeetCode
01

Problem Statement

Stones sit on integer points of a 2D plane, with at most one stone per point. Each stone is given as [x, y]: its row is x and its column is y.

A stone can be removed when another stone that is still on the plane shares its row or its column. You may remove stones one at a time, following that rule.

Return the largest number of stones you can remove.

Input

stones, a list of [x, y] points, one per stone.

Output

One integer: the most stones that can be removed.

Example 1
Input: stones = [[0,0], [0,1], [1,0], [1,2], [2,1], [2,2]]
Output: 5

All six stones are linked through shared rows and columns, so they form one group. Five can be removed, and one must stay.

Example 2
Input: stones = [[0,0], [0,2], [1,1], [2,0], [2,2]]
Output: 3

The stones at (0,0), (0,2), (2,0) and (2,2) form one group, so three of them can go. The stone at (1,1) shares no row or column with another stone, so it stays.

Example 3
Input: stones = [[0,0]]
Output: 0

A single stone cannot be removed, because no other stone shares its row or column.

Constraints
  • • 1 ≤ stones.length ≤ 1000
  • • 0 ≤ x, y ≤ 10^4
  • • No two stones are at the same point.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“same row or same column”
→ Two stones are linked through a shared line → make the rows and columns the DSU nodes.
“stone can be removed while another shares a line”
→ Each group must keep one stone, so the count is stones minus groups.
“largest number that can be removed”
→ Only the number of groups matters, not the order of removals.
03

Approach Complexity Ladder

Brute Force — compare every pair, then DFSO(N²)O(N²)

Link stones that share a row or column, count the groups with DFS, answer = stones − groups

Optimal — DSU over rows and columnsO(N · α(N))O(N)

Each stone unites its row with its column; answer = stones − distinct roots

Brute Force Intuition

Build a graph on the stones: two stones are neighbours when they share a row or a column. Count the connected groups with a DFS. Inside each group of k stones, k − 1 stones can be removed, so the answer is stones minus groups.

Recognition rule for this sub-pattern:

Start with n groups; each successful union reduces the count by one.
Remember this · Union-Find (DSU)
Trigger
Edges arrive one by one, "are these connected?", merge equivalent items, count components, extra edge creating a cycle.
Think
Am I repeatedly merging groups and asking whether two things are in the same group?
Key question
What are the elements, and what does it mean to union two of them?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Union-Find (DSU) translate into production architectures and low-level memory mechanics.