LeetCode 802. Find Eventual Safe States
原题链接在这里:https://leetcode.com/problems/find-eventual-safe-states/
题目:
In a directed graph, we start at some node and every turn, walk along a directed edge of the graph. If we reach a node that is terminal (that is, it has no outgoing directed edges), we stop.
Now, say our starting node is eventually safe if and only if we must eventually walk to a terminal node. More specifically, there exists a natural number K so that for any choice of where to walk, we must have stopped at a terminal node in less than K steps.
Which nodes are eventually safe? Return them as an array in sorted order.
The directed graph has N nodes with labels 0, 1, ..., 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.
Example:
Input: graph = [[1,2],[2,3],[5],[0],[5],[],[]]
Output: [2,4,5,6]
Here is a diagram of the above graph.

Note:
graphwill have length at most10000.- The number of edges in the graph will not exceed
32000. - Each
graph[i]will be a sorted list of different integers, chosen within the range[0, graph.length - 1].
题解:
If a node has no outgoing degree, then it must be safe.
Put these nodes into queue.
When polling cur node, from its incoming edge, decrease source node out degree by 1. If source node out degree becomes 0, then it is safe too, put it into queue.
Perform this unitl queue is empty. All the nodes coming out of queue are safe.
Time Complexity: O(V+E+VlogV). Construct rgraph takes O(V+E). Treverse takes O(V+E). Sort takes O(VlogV).
Space: O(V+E).
AC Java:
class Solution {
public List<Integer> eventualSafeNodes(int[][] graph) {
int N = graph.length;
List<List<Integer>> rgraph = new ArrayList<List<Integer>>();
for(int i = 0; i<N; i++){
rgraph.add(new ArrayList<Integer>());
}
LinkedList<Integer> que = new LinkedList<Integer>();
int [] outDegrees = new int[N];
for(int i = 0; i<N; i++){
outDegrees[i] = graph[i].length;
if(graph[i].length == 0){
que.add(i);
continue;
}
for(int target : graph[i]){
rgraph.get(target).add(i);
}
}
List<Integer> res = new ArrayList<Integer>();
while(!que.isEmpty()){
int cur = que.poll();
res.add(cur);
for(int source : rgraph.get(cur)){
outDegrees[source]--;
if(outDegrees[source] == 0){
que.add(source);
}
}
}
Collections.sort(res);
return res;
}
}
LeetCode 802. Find Eventual Safe States的更多相关文章
- [LeetCode] 802. Find Eventual Safe States 找到最终的安全状态
In a directed graph, we start at some node and every turn, walk along a directed edge of the graph. ...
- 【LeetCode】802. Find Eventual Safe States 解题报告(Python)
[LeetCode]802. Find Eventual Safe States 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...
- LC 802. Find Eventual Safe States
In a directed graph, we start at some node and every turn, walk along a directed edge of the graph. ...
- 【leetcode】802. Find Eventual Safe States
题目如下: 解题思路:本题大多数人采用DFS的方法,这里我用的是另一种方法.我的思路是建立一次初始值为空的safe数组,然后遍历graph,找到graph[i]中所有元素都在safe中的元素,把i加入 ...
- 802. Find Eventual Safe States
https://leetcode.com/problems/find-eventual-safe-states/description/ class Solution { public: vector ...
- [LeetCode] Find Eventual Safe States 找到最终的安全状态
In a directed graph, we start at some node and every turn, walk along a directed edge of the graph. ...
- [Swift]LeetCode802. 找到最终的安全状态 | Find Eventual Safe States
In a directed graph, we start at some node and every turn, walk along a directed edge of the graph. ...
- [LeetCode] 753. Cracking the Safe 破解密码
There is a box protected by a password. The password is n digits, where each letter can be one of th ...
- Java实现 LeetCode 802 找到最终的安全状态 (DFS)
802. 找到最终的安全状态 在有向图中, 我们从某个节点和每个转向处开始, 沿着图的有向边走. 如果我们到达的节点是终点 (即它没有连出的有向边), 我们停止. 现在, 如果我们最后能走到终点,那么 ...
随机推荐
- 使用SSM搭建一个简单的crud项目
使用SSM完成增删查改 前端使用到的技术:ajax,json,bootstrap等 完整项目地址:点这里GitHub 项目地址,可以在线访问 这一章节主要搭建SSM的环境. SpringMVC Spr ...
- 一秒可生成500万ID的分布式自增ID算法—雪花算法 (Snowflake,Delphi 版)
概述 分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的. 有些时候我们希望能使用一种 ...
- 全能中间件v19.5.7 正式版发布
v19.5.7 更新=========================1.新增 支持更多微信公众号API.2.优化 AccessToken 刷新机制.3.修复 微信公众号“消息加解密方式”为“安全模式 ...
- 二叉树根结点到任意结点的路径(C语言)
有一棵二叉树,如下图所示: 其中 # 表示空结点. 先序遍历:A B D E G C F 问题:怎么得到从根结点到任意结点的路径呢? 示例:输入 G,怎么得到从结点 A 到结点 G 的路径呢? 很明显 ...
- golang测试与性能调优
- Java Web报错:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
问题描述: 我们在用Eclipse进行Java web开发时,可能会出现这样的错误:The superclass javax.servlet.http.HttpServlet was not foun ...
- MongoDB的安装,mongod和mongo的区别
一. mongoDB安装路径 安装路径(最新4.0.11):https://www.mongodb.com/download-center/community?jmp=nav 建议另外找路径下载,外网 ...
- c# 拼接字符串根据逗号切割 后转换成集合或数组
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/qq_27559331/article/d ...
- java之mybatis之缓存
1.mybatis自带缓存功能.分为一级缓存,二级缓存. 2.一级缓存为 session 缓存,在一个 session中 ,一个查询的 select 语句只会执行一次,根据 <select&g ...
- WinForm下开发插件DevExpress安装及使用
WinForm下开发插件DevExpress安装及使用在Visual Studio中安装DevExpress开发插件插件的使用方法简单的Demo介绍下载链接:https://pan.baidu.com ...