原题链接在这里: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:

  • graph will have length at most 10000.
  • 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的更多相关文章

  1. [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.  ...

  2. 【LeetCode】802. Find Eventual Safe States 解题报告(Python)

    [LeetCode]802. Find Eventual Safe States 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...

  3. 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.  ...

  4. 【leetcode】802. Find Eventual Safe States

    题目如下: 解题思路:本题大多数人采用DFS的方法,这里我用的是另一种方法.我的思路是建立一次初始值为空的safe数组,然后遍历graph,找到graph[i]中所有元素都在safe中的元素,把i加入 ...

  5. 802. Find Eventual Safe States

    https://leetcode.com/problems/find-eventual-safe-states/description/ class Solution { public: vector ...

  6. [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.  ...

  7. [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.  ...

  8. [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 ...

  9. Java实现 LeetCode 802 找到最终的安全状态 (DFS)

    802. 找到最终的安全状态 在有向图中, 我们从某个节点和每个转向处开始, 沿着图的有向边走. 如果我们到达的节点是终点 (即它没有连出的有向边), 我们停止. 现在, 如果我们最后能走到终点,那么 ...

随机推荐

  1. GIL全局解释锁,死锁,信号量,event事件,线程queue,TCP服务端实现并发

    一.GIL全局解释锁 在Cpython解释器才有GIL的概念,不是python的特点 在Cpython解释器中,同一个进程下开启的多线程,同一时刻只能有一个线程执行,无法利用多核优势. 1.GIL介绍 ...

  2. k8s集群node节点一直NotReady, 且node节点(并非master)的kubelet报错:Unable to update cni config: No networks found in /etc/cni/net.d

    若要转载本文,请务必声明出处:https://www.cnblogs.com/zhongyuanzhao000/p/11401031.html 问题: 集群搭建的过程中,master节点初始化成功,但 ...

  3. 复制Linux虚拟机(VMware vSphere Client 工具)

    1.VMware vSphere Client 工具 登录,如下图          IP.用户名/密码均是物理机,登录完成界面: 2.选择一个复制的原虚拟机 A,点击左上角[文件]——导出——导出O ...

  4. 论DOM中文档和元素的位置大小属性及其区别

    element.offsetLeft/Top  获取元素相对于最近的有定位的父元素的坐标,如果没有有定位的父元素,则是文档坐标 element.scrollTop/Left 获取元素滚动卷去的距离 e ...

  5. 阿里巴巴 Java 开发手册(一):命名风格

    命名风格 1. [强制] 代码中的命名均不能以下划线或美元符号开始,也不能以下划线或美元符号结束. 反例: _name / __name / $Object / name_ / name$ / Obj ...

  6. ashx 接受 post json 请求

    HttpContext.Current.Response.ContentType = "application/json";            HttpContext.Curr ...

  7. MySQL数据库汇总

    -- mysql的最大连接数:默认为 100   -- mysql的增删改查   -- mysql统计各个字段(case when 用法 注:也可以使用其他的)   select (case when ...

  8. SG-UAP常用注解介绍

    注解基本介绍 Annotation(注解)是JDK5.0及以后版本引入的.它可以用于创建文档,跟踪代码中的依赖性,甚至执行基本编译时检查.注解是以‘@注解名’在代码中存在的,根据注解参数的个数,我们可 ...

  9. Windows10简单启动项目添加方法

    1,cmd输入 shell:startup 打卡启动项文件夹 2,将需要启动执行的项目的快捷方式放入此文件夹 3,重新启动系统就可以知道效果了

  10. Maven的SNAPSHOT版本找不到

    有时一个SNAPSHOT版本的包,明明打包部署到私服了,却还是报错找不到,比如: [WARNING] The POM for com.foo:bar:jar:0.4.0-20130404.093655 ...