Most Stones Removed with Same Row or Column
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.
stones, a list of [x, y] points, one per stone.
One integer: the most stones that can be removed.
All six stones are linked through shared rows and columns, so they form one group. Five can be removed, and one must stay.
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.
A single stone cannot be removed, because no other stone shares its row or column.
- • 1 ≤ stones.length ≤ 1000
- • 0 ≤ x, y ≤ 10^4
- • No two stones are at the same point.
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Link stones that share a row or column, count the groups with DFS, answer = stones − groups
Each stone unites its row with its column; answer = stones − distinct roots
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:
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.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Union-Find (DSU) translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.