> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Most Stones Removed with Same Row or Column

> Tested Python solution for LeetCode 947 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 947, [Medium](/catalog/medium). Topics: [Hash Table](/catalog/topics/hash-table), [Depth-First Search](/catalog/topics/depth-first-search), [Union-Find](/catalog/topics/union-find), [Graph Theory](/catalog/topics/graph-theory), Bipartite Graph. [View on LeetCode](https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 947   # by problem number
lcpy gen -s most_stones_removed_with_same_row_or_column   # by problem name
```

## 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

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

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

```
Input: stones = [[0,0]]
Output: 0
```

### 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](https://github.com/wislertt/leetcode-py/blob/main/leetcode/most_stones_removed_with_same_row_or_column/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/most_stones_removed_with_same_row_or_column/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n * alpha(n))
    # Space: O(n)
    def remove_stones(self, stones: list[list[int]]) -> int:
        parent: dict[tuple[str, int], tuple[str, int]] = {}

        def find(node: tuple[str, int]) -> tuple[str, int]:
            root = node
            while parent[root] != root:
                root = parent[root]
            while parent[node] != root:
                parent[node], node = root, parent[node]
            return root

        def union(a: tuple[str, int], b: tuple[str, int]) -> None:
            root_a, root_b = find(a), find(b)
            if root_a != root_b:
                parent[root_b] = root_a

        for x, y in stones:
            parent.setdefault(("r", x), ("r", x))
            parent.setdefault(("c", y), ("c", y))
            union(("r", x), ("c", y))

        roots = {find(("r", x)) for x, _ in stones}
        return len(stones) - len(roots)
```

## Complexity

| Time | Space |
| - | - |
| O(n \* alpha(n)) | O(n) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.