leetcode 947. Most Stones Removed with Same Row or Column

947. Most Stones Removed with Same Row or Column

Find connected components, with some specific connected component, the remove count is Num of elements - 1.

First method is to use BFS.

    class Solution {
        public int removeStones(int[][] stones) {
            int N = stones.length;
            int[] visited = new int[N];
            int ret = 0;
            Queue<Integer> q = new LinkedList<>();
            for (int i = 0; i < N; ++i) {
                if (visited[i] == 0) {
                    q.add(i);
                }
                int tmp = 0;
                while (!q.isEmpty()) {
                    Integer pos = q.poll();
                    int x = stones[pos][0], y = stones[pos][1];
                    if (visited[pos] == 1) continue;
                    tmp += 1;
                    visited[pos] = 1;
                    for (int j = 0; j < N; ++j) {
                        if (visited[j] == 0 && (stones[j][0] == x || stones[j][1] == y)) {
                            q.add(j);
                        }
                    }
                }
                ret += (tmp != 0 ? (tmp - 1) : 0);
            }
            return ret;
        }
    }

Also, for finding connected components problem. Union-find algorithm is an efficient algorithm.

And for simplicity, we only use path compression trick.

suppose x is connected components size

result is N - x

        class Solution {
        public int removeStones(int[][] stones) {
            DSU dsu = new DSU(20001);
            for (int i = 0; i < stones.length; ++i) {
                dsu.union(stones[i][0], 10000 + stones[i][1]);
            }
            Set<Integer> seen = new HashSet<>();
            for (int i = 0; i < stones.length; ++i) {
                seen.add(dsu.find(stones[i][0]));
            }
            return stones.length - seen.size();
        }
        
        static public class DSU {
            int[] parents;
            public DSU(int N) {
                parents = new int[N];
                for (int i = 0; i < N; ++i) parents[i] = i;
            }
            
            public int find(int x) {
                if (x != parents[x]) {
                    parents[x] = find(parents[x]);
                }
                return parents[x];
            }
            
            public void union(int x, int y) {
                parents[find(x)] = parents[find(y)];
            }
        }
    }

转载于:https://www.cnblogs.com/exhausttolive/p/10555956.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值