想要精通算法和SQL的成长之路 - 找到最终的安全状态

想要精通算法和SQL的成长之路 - 找到最终的安全状态

  • 前言
  • [一. 找到最终的安全状态](#一. 找到最终的安全状态)
    • [1.1 初始化邻接图](#1.1 初始化邻接图)
    • [1.2 构建反向邻接图](#1.2 构建反向邻接图)
    • [1.3 BFS遍历](#1.3 BFS遍历)
    • [1.4 完整代码](#1.4 完整代码)

前言

想要精通算法和SQL的成长之路 - 系列导航

一. 找到最终的安全状态

原题链接

我们从题目中可以看出来:

  • 出度为0的,就是终端节点。
  • 如果存在路径通向终端节点,那么该节点就是安全节点。那么终端节点本身也可以作为安全节点。
  • 而题目要求我们返回的是安全节点。
  • 满足题目要求的节点,一定是和终端节点相连的节点。

思路如下:

  1. 我们构建有向邻接图,并且统计出度。
  2. 出度为0的丢到队列中。
  3. 每层循环,处理出度为0的节点(终端节点),我们反向拿到它的前置节点(因此构建邻接图的时候要反向构建有向邻接图), 更新它的出度。若前置节点的出度为0,说明它之前就是一个安全节点,现在成为了终端节点。
  4. 遍历完毕之后,再遍历一遍出度数组,把所有出度为0的节点更新到结果集中即可。

1.1 初始化邻接图

java 复制代码
int n = graph.length;
// 初始化邻接图和出度数组
List<Integer>[] adj = new ArrayList[n];
int[] outDegree = new int[n];
for (int i = 0; i < n; i++) {
    adj[i] = new ArrayList<>();
}

1.2 构建反向邻接图

java 复制代码
// 构建邻接图和出度数组,这里的索引就是一条有向边的起点。
for (int i = 0; i < n; i++) {
    // 出度的个数,就是二维的长度
    outDegree[i] = graph[i].length;
    // 反向构建邻接图
    for (int j = 0; j < graph[i].length; j++) {
        adj[graph[i][j]].add(i);
    }
}

1.3 BFS遍历

java 复制代码
// 将出度为0的入队
LinkedList<Integer> queue = new LinkedList<>();
for (int i = 0; i < n; i++) {
    if (outDegree[i] == 0) {
        queue.offer(i);
    }
}
while (!queue.isEmpty()) {
    int size = queue.size();
    for (int i = 0; i < size; i++) {
        Integer cur = queue.poll();
        // adj[cur] 就是 pre --> 终端节点,拿到的所有 pre
        for (Integer pre : adj[cur]) {
            // 出度 -1,若为0,继续入队
            if (--outDegree[pre] == 0) {
                queue.offer(pre);
            }
        }
    }
}

1.4 完整代码

java 复制代码
public class Test802 {
    public List<Integer> eventualSafeNodes(int[][] graph) {
        int n = graph.length;
        // 初始化邻接图和出度数组
        List<Integer>[] adj = new ArrayList[n];
        int[] outDegree = new int[n];
        for (int i = 0; i < n; i++) {
            adj[i] = new ArrayList<>();
        }
        // 构建邻接图和出度数组,这里的索引就是一条有向边的起点。
        for (int i = 0; i < n; i++) {
            // 出度的个数,就是二维的长度
            outDegree[i] = graph[i].length;
            // 反向构建邻接图
            for (int j = 0; j < graph[i].length; j++) {
                adj[graph[i][j]].add(i);
            }
        }
        // 将出度为0的入队
        LinkedList<Integer> queue = new LinkedList<>();
        for (int i = 0; i < n; i++) {
            if (outDegree[i] == 0) {
                queue.offer(i);
            }
        }
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                Integer cur = queue.poll();
                // adj[cur] 就是 pre --> 终端节点,拿到的所有 pre
                for (Integer pre : adj[cur]) {
                    // 出度 -1,若为0,继续入队
                    if (--outDegree[pre] == 0) {
                        queue.offer(pre);
                    }
                }
            }
        }
        // 最终出度为0的全部加入到结果集中
        ArrayList<Integer> res = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            if (outDegree[i] == 0) {
                res.add(i);
            }
        }
        return res;
    }
}
相关推荐
查理零世26 分钟前
【蓝桥杯集训·每日一题2025】 AcWing 6134. 哞叫时间II python
python·算法·蓝桥杯
仟濹26 分钟前
【二分搜索 C/C++】洛谷 P1873 EKO / 砍树
c语言·c++·算法
紫雾凌寒35 分钟前
解锁机器学习核心算法|神经网络:AI 领域的 “超级引擎”
人工智能·python·神经网络·算法·机器学习·卷积神经网络
焱焱枫1 小时前
自适应SQL计划管理(Adaptive SQL Plan Management)在Oracle 12c中的应用
数据库·sql·oracle
2301_793069821 小时前
Spring Boot +SQL项目优化策略,GraphQL和SQL 区别,Spring JDBC 等原理辨析(万字长文+代码)
java·数据库·spring boot·sql·jdbc·orm
京东零售技术1 小时前
AI Agent实战:打造京东广告主的超级助手 | 京东零售技术实践
算法
火绒终端安全管理系统2 小时前
火绒终端安全管理系统V2.0【系统防御功能】
网络·安全·网络安全·火绒安全·火绒
weixin_387002152 小时前
Openssl之SM2加解密命令
安全·ubuntu·密码学·ssl·命令模式
MiyamiKK572 小时前
leetcode_位运算 190.颠倒二进制位
python·算法·leetcode
C137的本贾尼2 小时前
解决 LeetCode 串联所有单词的子串问题
算法·leetcode·c#