Skip to main content
LeetCode 947, Medium. Topics: Hash Table, Depth-First Search, Union-Find, Graph Theory, Bipartite Graph. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 20 parametrized pytest cases, and a playground notebook:

Problem

On a 2D plane, we place n stones at some integer coordinate points. Each coordinate point may have at most one stone. A stone can be removed if it shares either the same row or the same column as another stone that has not been removed. Given an array stones of length n where stones[i] = [x<sub>i</sub>, y<sub>i</sub>] represents the location of the i<sup>th</sup> stone, return the largest possible number of stones that can be removed.

Examples

Constraints

  • 1 <= stones.length <= 1000
  • 0 <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup>
  • No two stones are at the same coordinate point.

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

Last modified on September 7, 2026