并查集练习 — 扩展问题(一)

根据岛屿问题二的问题进行扩展。
如果给定的m * n太大了而positions[][]中的元素个数很少怎么办 ? 比如说mn是100W * 100W的大小,那是不是每次初始化时,给定的数组空间太大了。这种时候改怎么进行优化?

可以尝试并查集介绍及原理中介绍的Map结构。
将postion中的 i 和 j ,拼接成Map中的Key,而后看parentMap中是否存在该Key,来确定是否要进行union操作。

代码

public static List<Integer> numIslands22(int m, int n, int[][] positions) {
        List<Integer> ans = new ArrayList<>();
        List<String> keys = new ArrayList<>();
        
 		//key : r_c拼接
        for (int[] position : positions) {
            keys.add(position[0] + "_" + position[1]);
        }
        UnionFind2 uf2 = new UnionFind2(keys);
        for (int[] position : positions) {
            ans.add(uf2.connect(position[0], position[1]));
        }
        return ans;
    }

   
    public static class UnionFind2 {
        HashMap<String, String> parent;
        HashMap<String, Integer> sizeMap;
        int sets;

        public UnionFind2(List<String> keys) {

            parent = new HashMap<>();
            sizeMap = new HashMap<>();
            sets = 0;
            for (String key : keys) {
                parent.put(key, key);
                sizeMap.put(key, 1);
            }
        }

        public int connect(int m, int n) {
            String key = String.valueOf(m) + "_" + String.valueOf(n);

            if (!parent.containsKey(key)) {
                parent.put(key, key);
                sizeMap.put(key, 1);
                sets++;
                String keyUp = String.valueOf(m - 1) + "_" + String.valueOf(n);
                String keyDown = String.valueOf(m + 1) + "_" + String.valueOf(n);
                String keyLeft = String.valueOf(m) + "_" + String.valueOf(n - 1);
                String keyRight = String.valueOf(m) + "_" + String.valueOf(n + 1);

                union(key, keyUp);
                union(key, keyDown);
                union(key, keyLeft);
                union(key, keyRight);
            }
            return sets;
        }

        public String findParent(String key) {
            Stack<String> stack = new Stack<>();
            while (key != parent.get(key)) {
                stack.push(key);
                key = parent.get(key);
            }

            while (!stack.isEmpty()) {
                parent.put(stack.pop(), key);
            }
            return key;
        }

        public void union(String currentKey, String aroundKey) {
            if (!parent.containsKey(currentKey) || !parent.containsKey(aroundKey)) {
                return;
            }

            String key1 = findParent(currentKey);
            String key2 = findParent(aroundKey);
            if (key1 != key2) {
                Integer size1 = sizeMap.get(key1);
                Integer size2 = sizeMap.get(key2);

                if (size1 >= size2) {
                    parent.put(key2, key1);
                    sizeMap.put(key1, size1 + size2);
                } else {
                    parent.put(key1, key2);
                    sizeMap.put(key2, size1 + size2);
                }
                sets--;
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值