[LeetCode] 947. Most Stones Removed with Same Row or Column

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] = [xi, yi] represents the location of the ith stone, return the largest possible number of stones that can be removed.

Example 1:

Input: stones = [[0,0],[0,1],[1,0],[1,2],[2,1],[2,2]]
Output: 5
Explanation: One way to remove 5 stones is as follows:
1. Remove stone [2,2] because it shares the same row as [2,1].
2. Remove stone [2,1] because it shares the same column as [0,1].
3. Remove stone [1,2] because it shares the same row as [1,0].
4. Remove stone [1,0] because it shares the same column as [0,0].
5. Remove stone [0,1] because it shares the same row as [0,0].
Stone [0,0] cannot be removed since it does not share a row/column with another stone still on the plane.

Example 2:

Input: stones = [[0,0],[0,2],[1,1],[2,0],[2,2]]
Output: 3
Explanation: One way to make 3 moves is as follows:
1. Remove stone [2,2] because it shares the same row as [2,0].
2. Remove stone [2,0] because it shares the same column as [0,0].
3. Remove stone [0,2] because it shares the same row as [0,0].
Stones [0,0] and [1,1] cannot be removed since they do not share a row/column with another stone still on the plane.

Example 3:

Input: stones = [[0,0]]
Output: 0
Explanation: [0,0] is the only stone on the plane, so you cannot remove it.

Constraints:

  • 1 <= stones.length <= 1000
  • 0 <= xi, yi <= 104
  • No two stones are at the same coordinate point.

移除最多的同行或同列石头。

n 块石头放置在二维平面中的一些整数坐标点上。每个坐标点上最多只能有一块石头。

如果一块石头的 同行或者同列 上有其他石头存在,那么就可以移除这块石头。

给你一个长度为 n 的数组 stones ,其中 stones[i] = [xi, yi] 表示第 i 块石头的位置,返回 可以移除的石子 的最大数量。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/most-stones-removed-with-same-row-or-column
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题目蛮有意思,注意理解这里移除石头的规则。我参考了这个帖子

 

这道题最终的目的是让我们尽可能多地移除石头,从而使剩下的石头数量最少。移除的规则是如果一块石头的同行或者同列上有其他石头存在,那么就可以移除这块石头。这个题设会比较容易想到 union find。利用 union find,我们可以把同行和同列的石头做成一个连通分量。对于同一个连通分量里的石头,无论怎样移除,最后都还需要剩下一个,就代表当前这个连通分量。所以可以移除的石头数量 = 石头的总数量 - 图中所有的连通分量。

时间O(nlogn) - O(nlog(A)),其中 n 为石子的数量,A 是数组 stones 里横纵坐标不同值的总数

空间O(n) - hashmap

Java实现

 1 class Solution {
 2     public int removeStones(int[][] stones) {
 3         UnionFind uf = new UnionFind();
 4         for (int[] stone : stones) {
 5             uf.union(stone[0] + 10001, stone[1]);
 6         }
 7         return stones.length - uf.getCount();
 8     }
 9 
10     private class UnionFind {
11         private HashMap<Integer, Integer> parent;
12         private int count;
13 
14         public UnionFind() {
15             this.parent = new HashMap<>();
16             this.count = 0;
17         }
18 
19         public int getCount() {
20             return count;
21         }
22 
23         public int find(int x) {
24             if (!parent.containsKey(x)) {
25                 parent.put(x, x);
26                 // 并查集集中新加入一个结点,结点的父亲结点是它自己,所以连通分量的总数 +1
27                 count++;
28             }
29             if (x != parent.get(x)) {
30                 parent.put(x, find(parent.get(x)));
31             }
32             return parent.get(x);
33         }
34         
35         public void union(int x, int y) {
36             int rootX = find(x);
37             int rootY = find(y);
38             if (rootX == rootY) {
39                 return;
40             }
41             parent.put(rootX, rootY);
42             // 两个连通分量合并成为一个,连通分量的总数 -1
43             count--;
44         }
45     }
46 }

 

LeetCode 题目总结

posted @ 2022-11-15 14:24  CNoodle  阅读(74)  评论(0编辑  收藏  举报