LeetCode 947 Most Stones Removed with Same Row or Column (并查集)

On a 2D plane, we place stones at some integer coordinate points.  Each coordinate point may have at most one stone.

Now, a move consists of removing a stone that shares a column or row with another stone on the grid.

What is the largest possible number of moves we can make?

 

Example 1:

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

Example 2:

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

Example 3:

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

 

Note:

  1. 1 <= stones.length <= 1000
  2. 0 <= stones[i][j] < 10000

题目链接:https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/

题目分析:题意是一个点必须所在行或列还有别的点才能删,问最多能删多少次,很明显就是把在同一行或列的点都放到一个set里,然后每个set最后只会留一个元素,所以答案就是n-set的大小,并查集维护

3ms,时间击败98.87%

class Solution {
    
    class UnionFind {
        int n;
        int[] fa;
        UnionFind(int n) {
            this.n = n;
            this.fa = new int[n];
            for (int i = 0; i < n; i++) {
                this.fa[i] = i;
            }
        }
        
        int find(int x) {
            if (x == this.fa[x]) {
                return x;
            }
            this.fa[x] = find(this.fa[x]);
            return this.fa[x];
        }
        
        void union(int a, int b) {
            int r1 = find(a);
            int r2 = find(b);
            if (r1 != r2) {
                this.fa[r1] = r2;
            }
        }
        
        int getComponents() {
            int ans = 0;
            for (int i = 0; i < n; i++) {
                if (find(i) == i) {
                    ans++;
                }
            }
            return ans;
        }
    }
    
    public int removeStones(int[][] stones) {
        UnionFind solver = new UnionFind(stones.length);
        // for (int i = 0; i < stones.length; i++) {
        //     for (int j = i + 1; j < stones.length; j++) {
        //         if (stones[i][0] == stones[j][0] || stones[i][1] == stones[j][1]) {
        //             solver.union(i, j);
        //         }
        //     }
        // }
        int[] preRow = new int[10000];
        int[] preCol = new int[10000];
        for (int i = 0; i < stones.length; i++) {
            if (preRow[stones[i][0]] == 0) {
                preRow[stones[i][0]] = i + 1;
            } else {
                solver.union(preRow[stones[i][0]] - 1, i);
            }
            
            if (preCol[stones[i][1]] == 0) {
                preCol[stones[i][1]] = i + 1;
            } else {
                solver.union(preCol[stones[i][1]] - 1, i);
            }
        }
        
        return stones.length - solver.getComponents();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值