[LeetCode] 802. Find Eventual Safe States
We start at some node in a directed graph, and every turn, we walk along a directed edge of the graph. If we reach a terminal node (that is, it has no outgoing directed edges), we stop.
We define a starting node to be safe if we must eventually walk to a terminal node. More specifically, there is a natural number k
, so that we must have stopped at a terminal node in less than k
steps for any choice of where to walk.
Return an array containing all the safe nodes of the graph. The answer should be sorted in ascending order.
The directed graph has n
nodes with labels from 0
to n - 1
, where n
is the length of graph
. The graph is given in the following form: graph[i]
is a list of labels j
such that (i, j)
is a directed edge of the graph, going from node i
to node j
.
Example 1:
Input: graph = [[1,2],[2,3],[5],[0],[5],[],[]] Output: [2,4,5,6] Explanation: The given graph is shown above.
Example 2:
Input: graph = [[1,2,3,4],[1,2],[3,4],[0,4],[]] Output: [4]
Constraints:
n == graph.length
1 <= n <= 104
0 <= graph[i].length <= n
graph[i]
is sorted in a strictly increasing order.- The graph may contain self-loops.
- The number of edges in the graph will be in the range
[1, 4 * 104]
.
找到最终的安全状态。
在有向图中,以某个节点为起始节点,从该点出发,每一步沿着图中的一条有向边行走。如果到达的节点是终点(即它没有连出的有向边),则停止。
对于一个起始节点,如果从该节点出发,无论每一步选择沿哪条有向边行走,最后必然在有限步内到达终点,则将该起始节点称作是 安全 的。
返回一个由图中所有安全的起始节点组成的数组作为答案。答案数组中的元素应当按 升序 排列。
该有向图有 n 个节点,按 0 到 n - 1 编号,其中 n 是 graph 的节点数。图以下述形式给出:graph[i] 是编号 j 节点的一个列表,满足 (i, j) 是图的一条有向边。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/find-eventual-safe-states
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
这是一道图论的题,这里我提供一个DFS的做法。题目说了半天,精简下来其实题目问的是如果你从图中的每个节点都试图出发,是否有可能在走了若干步之后在某个点就停下了,即停下的点是没有 next 节点的。题意反过来理解,就是试图找图中从每个点出发是否存在环,把没有环的起点按照升序排列输出即可。
找环这里我采用的是染色法,建立一个和 input 数组等长的 color 数组,里面代表的是每个节点的颜色情况。一开始初始化为 0,说明节点还未被访问过。用 DFS 开始遍历,一开始把当前点标记为 2,意思是这个点有可能最后是环的一部分。接着往下递归遍历的时候,如果返回的结果是 false 则说明这一溜下去的点都在环上,就都需要被标记成 2;反之如果递归的结果不是 false,则说明从当前点开始是可以走到某个终点的,我们再把当前点的颜色变为 1。
时间O(n)
空间O(n)
Java实现
1 class Solution { 2 public List<Integer> eventualSafeNodes(int[][] graph) { 3 List<Integer> res = new ArrayList<>(); 4 // corner case 5 if (graph == null || graph.length == 0) { 6 return res; 7 } 8 9 // normal case 10 int count = graph.length; 11 int[] color = new int[count]; 12 for (int i = 0; i < count; i++) { 13 if (dfs(graph, i, color)) { 14 res.add(i); 15 } 16 } 17 return res; 18 } 19 20 private boolean dfs(int[][] graph, int start, int[] color) { 21 if (color[start] != 0) { 22 return color[start] == 1; 23 } 24 color[start] = 2; 25 for (int next : graph[start]) { 26 if (!dfs(graph, next, color)) { 27 return false; 28 } 29 } 30 color[start] = 1; 31 return true; 32 } 33 }
原文地址:https://www.cnblogs.com/cnoodle/p/15102763.html
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 记一次 Kafka Producer 性能调优实战
- Kafka 独立消费者
- 【iOS】记录iOS14以及xcode12 遇到的问题
- 如果你还在犹豫要不要入行,请先看看我的IT入坑记【技术创作101训练营】
- Session、Cookie、Token 【浅谈三者之间的那点事】
- Python 微信机器人-20行代码实现斗图功能,简单易懂,全是干货!斗图啦API调用方法
- 浅谈布隆过滤器
- Python 技术篇-获取图片GPS信息,锁定追踪图片拍摄地点、拍摄时间
- 测试工具 - Postman接口测试入门使用手册,Postman如何进行数据关联、自动更新cookies、简单编程
- 白盒测试工具 - sonar的安装、配置与使用入门手册,用sonar检查代码质量实战演示
- Chmod -R 777 / 误操作恢复教程
- 最全总结 | 聊聊 Python 数据处理全家桶(Redis篇)
- 虚拟机安装mikrotik-ROS
- 搬砖武士|手把手教你在容器服务 TKE 上使用 LB直通 Pod
- linux安装snmp服务-ubuntu