原题链接在这里: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. Python学习之路:列表(List)的append()、extend()与insert()方法

    相同点 这三种方法的作用都是为列表(List)添加值 它们的语法为: list.append(obj)list.extend(seq)list.insert(index,obj) #此处index为对 ...

  2. Java自学-控制流程 for

    Java的for循环 for循环,和while一样,只是表达方式不一样 示例 1 : for 比较for和while public class HelloWorld { public static v ...

  3. 【前端】将前台table数据导出excel表格

    1.首先引用jquery以及table2excel <script type="text/javascript" src="js/jquery.table2exce ...

  4. 英语dyamaund钻石

    dyamaund  英文词汇,中文翻译为金刚石的;镶钻;用钻石装饰 中文名:镶钻;钻石装饰 外文名:dyamaund 目录 释义 dyamaund 读音:[ˈdaɪəmənd, ˈdaɪmənd] ...

  5. 如何统一管理单个任务下所有API的同步情况?

    1. 一分钟完成单个API配置 单个API的配置包含:API名称.URL地址.请求方式.参数设置.自定义高级设置. 参数允许用户填写:Text.WebService.Timestamp.DependO ...

  6. 自旋锁spin_lock、spin_lock_irq 和 spin_lock_irqsave

    自旋锁和互斥锁的区别是,自旋锁不会引起睡眠,所以可用于不能休眠的代码中(如IRQ) 自旋锁保持期间抢占失效,而信号量保持期间可以被抢占 定义 spinlock_t lock; init #define ...

  7. java-ExceptionHandler全局异常处理

    springmvc配置文件: <!-- 定义全局异常处理,只有一个全局异常处理器起作用 --> <bean id="exceptionResolver" clas ...

  8. ES读写索引内幕分析

    一.简介 ES中的索引都进行分片,每个分片都会保存多个副本.这些副本称为复制组,在添加或删除索引时必须同步副本.如果不这样,从不同的副本中读取的索引可能截然不同.保持分片副本同步并从中提供读取的过程被 ...

  9. 1M大概多少个字

    <?php echo strlen("你"); 保存文件为gbk 输出2 保存文件为utf-8 输出3 说明不同编码占用字节不同 1M=1024kB 1KB = 1024B ...

  10. 关于如何往Jupyter notebook添加可选的kernel

    关于如何往Jupyter notebook添加可选的kernel 1. Anaconda知识预热 管理虚拟环境 关于如何安装Anaconda,这里就不再一一赘述,安装完Anaconda,接下来我们就可 ...