【算法&面试】并查集开干!

记录并查集的原因

这三天,做了两道并查集的题,力扣签到题,两道都没做出来。每次都是基本思路有了,但是实现的时候,要么是脑海中没有找到合适的数据结构,要么就是感觉的选中的数据结构缺了点什么。上次做并查集题的时候,由于突然有点急事就放下了,今天,我再次遇到了这种类型的题。

此时我意识到,这道题对我而言,是一道好题!可以让我再次得到一种套路,一种数据结构思维。

resolve:决心(re:再一次,solve解决)。

我已经能想象到并查集臭弟弟在我面前哭泣的样子了。

开始专注于并查集训练了。刷了50+贪心算法,我才感受到贪心思维。而并查集应该不需要思维而是套模板,我希望能在20道题内,把它用熟!
力扣并查集

并查集是什么?

是一种有关图的算法

它把一堆东西,一堆元素,分开,组合。满足某种组合条件的元素,在实现上,给其一个固定的father、parent。

连通分量:串连通数据、一组满足需要规则的数据。

有关并查集的题,使用广度优先遍历和深度优先遍历都可以解决。但是这种暴力求解方法,在适合于并查集的题目中,常常时间复杂度,空间复杂度都会非常高。

并查集1:684. 冗余连接

image.png

class Solution {
    private int[] result = new int[2];
    public int[] findRedundantConnection(int[][] edges) {
        int n = edges.length;
        int[] parent = new int[n + 1];
        for(int i = 0; i < n + 1; i++){
            parent[i] = i;
        }
        for(int i = 0; i < n; ++i){
            merge(parent, edges[i][0], edges[i][1]);
        }
        return result;
    }
    public void merge(int[] parent, int a, int b) {
        int rootA = find(parent, a);
        int rootB = find(parent, b);
        //相连的情况,a和b都存在于这个一个连通分量
        if(rootA == rootB){
            result[0] = a;
            result[1] = b;
            return ;
        }
        //a和b直接成为一对连通分量
        parent[rootA] = rootB;
    }
    public int find(int[] parent, int a){
        //如果a==parent[a]那么说明它是根!
        if(a != parent[a]){
            parent[a] = find(parent, parent[a]);
        }
        return parent[a];
    }
}

并查集2:947. 移除最多的同行或同列石头

image.png

使用map的并查集
import java.util.HashMap;
import java.util.Map;

public class Solution {

    public int removeStones(int[][] stones) {
        UnionFind unionFind = new UnionFind();

        for (int[] stone : stones) {
            // 下面这三种写法任选其一
            // unionFind.union(~stone[0], stone[1]);
            // unionFind.union(stone[0] - 10001, stone[1]);
            unionFind.union(stone[0] + 10001, stone[1]);
        }
        return stones.length - unionFind.getCount();
    }

    private class UnionFind {

        private Map<Integer, Integer> parent;
        private int count;

        public UnionFind() {
            this.parent = new HashMap<>();
            this.count = 0;
        }

        public int getCount() {
            return count;
        }

        public int find(int x) {
            //行坐标加了10001与列坐标做区分
            //如果map中不存在这个元素,则直接添加
            if (!parent.containsKey(x)) {
                parent.put(x, x);
                count++;
            }
            //如果map中已经存在了x,且x不是自己的根,则递归,得到x真实的根(下面合并操作会导致x的根不一致,这一步让x的根一致)
            //时间复杂度最多为3,因为它
            if (x != parent.get(x)) {
                //存的是每一个连通分量的头
                //如果出现合并的情况,这个操作,也将递归把两个连通分量的头部统一
                parent.put(x, find(parent.get(x)));
            }
            //返回x的真实根
            return parent.get(x);
        }

        public void union(int x, int y) {
            int rootX = find(x);
            int rootY = find(y);
            //存好了,正好有个连通分量同时有x或y。所以此时的rootX和rootY都是该连通分量第一个元素
            if (rootX == rootY) {
                return;
            }
            //这种情况就是x和y属于不同的连通分量(可能是它自己)。
            //此时,就是直接存进去。然后连通分量数量-1
            //之后如果遇到rootX中的所有元素,这个时候这些元素的根value都将在find方法中变成rootY
            parent.put(rootX, rootY);
            count--;
        }
    }
}
不使用map的并查集,时间直接减半
class Solution {
    class DJS {
        DJS p;

        DJS(){
            this.p = this;
        }

        DJS find(){
            if(p.p != p) {
                p = p.find();
            }
            return p;
        }

        void union(DJS b) {
            this.find().p = b;//不按rank会简单一些 就一些
        }
    }

    public int removeStones(int[][] stones) {
        //并查集
        DJS[] unions = new DJS[20000];
        int ret = 0;
        for(int[] t : stones) {
            int index1 = t[0], index2 = t[1] + 10000;
            DJS d1 = unions[index1], d2 = unions[index2];
            if(d1 == null && d2 == null) {
                DJS d = new DJS();
                unions[index1] = d;
                unions[index2] = d;
            } else if(d1 == null) {
                ++ret;
                d1 = new DJS();
                d1.union(d2);//放入d2的连通分量头
                unions[index1] = d1;
            } else if(d2 == null) {
                ++ret;
                d2 = new DJS();
                d2.union(d1);//放入d1的连通分量头
                unions[index2] = d2;
            } else {
                //都存在,且在不同的连通分量中,则直接连接这两个元素即可
                ++ret;
                if(d1.find() != d2.find()) {
                    ++ret;
                    d1.union(d2);
                }
            }
        }

        return ret;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值