|
| 1 | +947\. Most Stones Removed with Same Row or Column |
| 2 | + |
| 3 | +Medium |
| 4 | + |
| 5 | +On a 2D plane, we place `n` stones at some integer coordinate points. Each coordinate point may have at most one stone. |
| 6 | + |
| 7 | +A stone can be removed if it shares either **the same row or the same column** as another stone that has not been removed. |
| 8 | + |
| 9 | +Given an array `stones` of length `n` where <code>stones[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents the location of the <code>i<sup>th</sup></code> stone, return _the largest possible number of stones that can be removed_. |
| 10 | + |
| 11 | +**Example 1:** |
| 12 | + |
| 13 | +**Input:** stones = [[0,0],[0,1],[1,0],[1,2],[2,1],[2,2]] |
| 14 | + |
| 15 | +**Output:** 5 |
| 16 | + |
| 17 | +**Explanation:** One way to remove 5 stones is as follows: |
| 18 | + |
| 19 | +1. Remove stone [2,2] because it shares the same row as [2,1]. |
| 20 | +2. Remove stone [2,1] because it shares the same column as [0,1]. |
| 21 | +3. Remove stone [1,2] because it shares the same row as [1,0]. |
| 22 | +4. Remove stone [1,0] because it shares the same column as [0,0]. |
| 23 | +5. Remove stone [0,1] because it shares the same row as [0,0]. |
| 24 | + |
| 25 | +Stone [0,0] cannot be removed since it does not share a row/column with another stone still on the plane. |
| 26 | + |
| 27 | +**Example 2:** |
| 28 | + |
| 29 | +**Input:** stones = [[0,0],[0,2],[1,1],[2,0],[2,2]] |
| 30 | + |
| 31 | +**Output:** 3 |
| 32 | + |
| 33 | +**Explanation:** One way to make 3 moves is as follows: |
| 34 | + |
| 35 | +1. Remove stone [2,2] because it shares the same row as [2,0]. |
| 36 | + |
| 37 | +2. Remove stone [2,0] because it shares the same column as [0,0]. |
| 38 | + |
| 39 | +3. Remove stone [0,2] because it shares the same row as [0,0]. |
| 40 | + |
| 41 | +Stones [0,0] and [1,1] cannot be removed since they do not share a row/column with another stone still on the plane. |
| 42 | + |
| 43 | +**Example 3:** |
| 44 | + |
| 45 | +**Input:** stones = [[0,0]] |
| 46 | + |
| 47 | +**Output:** 0 |
| 48 | + |
| 49 | +**Explanation:** [0,0] is the only stone on the plane, so you cannot remove it. |
| 50 | + |
| 51 | +**Constraints:** |
| 52 | + |
| 53 | +* `1 <= stones.length <= 1000` |
| 54 | +* <code>0 <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code> |
| 55 | +* No two stones are at the same coordinate point. |
0 commit comments