2024.4.17力扣每日一题——尽量减少恶意软件的传播 II

题目来源

力扣每日一题;题序:928

我的题解

方法一 深度优先搜索

依次遍历 initial中的节点,记当前遍历的节点为 x,同时使用深度优先搜索算法标记从节点 x 可以访问到的节点集 infectedSet;如果节点 u∈infectedSet,那么将节点x加入到 infectedBy[u]中。
遍历结束后,使用 count[x] 统计只能被节点 x 感染到的所有节点数目:如果 infectedBy[u]的大小为 1,且 infectedBy[u][0]=x,那么节点 u就是只能被节点 x 感染到的节点。最后遍历 count数组,取使 count[x] 最大且下标值最小的节点 x 为答案。
就是求每个感染节点可以影响的节点有哪些?然后再看只会被一个感染节点感染的节点有哪些?

时间复杂度: O( n 3 n^3 n3)
空间复杂度:O( n 2 n^2 n2)

class Solution {
    public int minMalwareSpread(int[][] graph, int[] initial) {
        int n=graph.length;
        boolean[] isInitial=new boolean[n];
        List<Integer>[] infectedBy=new ArrayList[n];
        for(int i=0;i<n;i++){
            infectedBy[i]=new ArrayList<>();
        }
        for(int x:initial){
            isInitial[x]=true;
        }
        for(int x:initial){
            boolean[] visited=new boolean[n];
            //深搜计算x会感染的节点数
            dfs(graph,visited,isInitial,x);
            //那些节点会被x感染
            for(int u=0;u<n;u++){
                if(visited[u])
                    infectedBy[u].add(x);
            }
        }
        //计算只能被节点i感染的节点数目
        int[] count=new int[n];

        for(int u=0;u<n;u++){
            if(infectedBy[u].size()==1){
                count[infectedBy[u].get(0)]++;
            }
        }
        int res=initial[0];
        for(int x:initial){
            if(count[x]>count[res]||count[x]==count[res]&&x<res)
                res=x;
        }
        return res;
    }

    public void dfs(int[][]  graph,boolean[] visited,boolean[] isInitial,int x){
        int n=graph.length;
        for(int next=0;next<n;next++){
            if(graph[x][next]==0||isInitial[next]||visited[next]){
                continue;
            }
            visited[next]=true;
            dfs(graph,visited,isInitial,next);
        }
    }
}
方法二 广度优先搜索

与方法一类似,只是使用广度优先搜索来获取每个感染节点可以感染的节点。

时间复杂度:O( n 3 n^3 n3)
空间复杂度:O( n 2 n^2 n2)

class Solution {
    public int minMalwareSpread(int[][] graph, int[] initial) {
        int n=graph.length;
        boolean[] isInitial=new boolean[n];
        List<Integer>[] infectedBy=new ArrayList[n];
        for(int i=0;i<n;i++){
            infectedBy[i]=new ArrayList<>();
        }
        for(int x:initial){
            isInitial[x]=true;
        }
        for(int x:initial){
            boolean[] visited=new boolean[n];
            //深搜计算x会感染的节点数
            bfs(graph,visited,isInitial,x);
            //那些节点会被x感染
            for(int u=0;u<n;u++){
                if(visited[u])
                    infectedBy[u].add(x);
            }
        }
        //计算只能被节点i感染的节点数目
        int[] count=new int[n];

        for(int u=0;u<n;u++){
            if(infectedBy[u].size()==1){
                count[infectedBy[u].get(0)]++;
            }
        }
        int res=initial[0];
        for(int x:initial){
            if(count[x]>count[res]||count[x]==count[res]&&x<res)
                res=x;
        }
        return res;
    }

    public void bfs(int[][]  graph,boolean[] visited,boolean[] isInitial,int x){
        int n=graph.length;
        Queue<Integer> queue=new LinkedList<>();
        queue.offer(x);
        while(!queue.isEmpty()){
            int sz=queue.size();
            for(int i=0;i<sz;i++){
                int t=queue.poll();
                for(int next=0;next<n;next++){
                    if(graph[t][next]==0||isInitial[next]||visited[next]){
                        continue;
                    }
                    visited[next]=true;
                    queue.offer(next);
                }
            }
        }
        
    }
}
方法三 并查集

使用并查集来优化。使用图 G 的所有节点初始化并查集 uf。在遍历 initial 中的节点时,记当前遍历的节点为 v,只需要遍历所有图 G 中与 v 连接的节点,并将节点对应的子集加入到 infectedSet 中;对于子集 u∈infectedSet,那么将节点 v 加入到 infectedBy[u]中。
遍历结束后,使用 count[v] 统计图 G 中只能被节点 v 感染到的所有节点数目:如果 infectedBy[u] 的大小为 111,且 infectedBy[u][0]=v,那么子集 u 的所有节点都只能被节点 v 感染到; 遍历所有节点,如果节点 w 在子集 u 中,那么该节点就是图 G中只能被节点 v 感染到的节点。最后遍历 count数组,取使 count[v] 最大且下标值最小的节点 v为答案。

时间复杂度:O( n 2 l o g n n^2logn n2logn)
空间复杂度:O( n 2 n^2 n2)

class Solution {
    public int minMalwareSpread(int[][] graph, int[] initial) {
        int n=graph.length;
        boolean[] isInitial=new boolean[n];
        for(int x:initial){
            isInitial[x]=true;
        }
        UF uf=new UF(n);
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(i!=j&&graph[i][j]==1&&!isInitial[i]&&!isInitial[j]){
                    uf.union(i,j);
                }
            }
        }
        List<Integer>[] infectedBy=new ArrayList[n];
        for(int i=0;i<n;i++){
            infectedBy[i]=new ArrayList<>();
        }
        for(int x:initial){
            boolean[] visited=new boolean[n];
            for(int u=0;u<n;u++){
                if(isInitial[u]||graph[u][x]==0)
                    continue;
                visited[uf.find(u)]=true;
            }
            //那些节点会被x感染
            for(int u=0;u<n;u++){
                if(visited[u])
                    infectedBy[u].add(x);
            }
        }
        //计算只能被节点i感染的节点数目
        int[] count=new int[n];

        for(int u=0;u<n;u++){
            if(infectedBy[u].size()==1){
                for(int w=0;w<n;w++){
                    if(uf.find(u)==uf.find(w))
                        count[infectedBy[u].get(0)]++;
                }
                
            }
        }
        int res=initial[0];
        for(int x:initial){
            if(count[x]>count[res]||count[x]==count[res]&&x<res)
                res=x;
        }
        return res;
    }

    class UF{
        private int count;
        private int parent[];

        public UF(int n){
            count=n;
            parent=new int[n];
            for (int i = 0; i < n; i++) {
                parent[i]=i;
            }
        }

        public void union(int p,int q){
            int parentP=find(p);
            int parentQ=find(q);
            if (parentP==parentQ)
                return;
            parent[parentQ]=parentP;
            count--;
        }

        public boolean isConnection(int p,int q){
            int parentP=find(p);
            int parentQ=find(q);
            return parentP==parentQ;
        }

        public int find(int x){
            if(parent[x]!=x){
                parent[x]=find(parent[x]);//路径压缩
            }
            return parent[x];
        }

        public int getCount(){
            return count;
        }
    }
}

有任何问题,欢迎评论区交流,欢迎评论区提供其它解题思路(代码),也可以点个赞支持一下作者哈😄~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜菜的小彭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值