原题链接在这里: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. JAVA知识点总结篇(三)

    抽象类 使用规则 abstract定义抽象类: abstract定义抽象方法,只有声明,不需要实现: 包含抽象方法的类是抽象类: 抽象类中可以包含普通方法,也可以没有抽象方法: 抽象类不能直接创建,可 ...

  2. PB 点击标题行排序和双击打开编辑页面共存不冲突的方法

    根据doubleclicked() 事件的参数 row 进行判断 大于0才进入编辑页面(不能用getrow()事件获取行id,双击标题行获取的是1) if row>0 then event ue ...

  3. AJAX调用数据,滚动到底部

    最近一个小项目里面,需要使用AJAX去拉取数据,并且直接显示最后一条信息,也就是滚动到底部.实现脚本如下: var scrollHeight = $('.txtBox3').prop("sc ...

  4. 查看Linux服务器配置

    1.查看CPU lscpu 2.查看内存 free -g 或 free -m 3.查看硬盘 df -h

  5. HttpClient参观记:.net core 2.2 对HttpClient到底做了神马

    .net core 于 10月17日发布了 ASP.NET Core 2.2.0 -preview3,在这个版本中,我看到了一个很让我惊喜的新特性:HTTP Client Performance Im ...

  6. java之mybatis之占位符

    1.mybatis中有两种占位符 #{}和 ${}. 2. #{} 占位符是为了获取值,获取的值用在 where 语句后,insert 语句后,update 语句. #{} 获取值,是根据值的名称取值 ...

  7. pandas-07 DataFrame修改index、columns名的方法

    pandas-07 DataFrame修改index.columns名的方法 一般常用的有两个方法: 1.使用DataFrame.index = [newName],DataFrame.columns ...

  8. 3.用Python画五角星

    import turtleimport timeimport os #def draw_square(org_x, org_y, x, y): turtle.setpos(org_x, org_y) ...

  9. kali安装结束重启黑屏?

    很多人碰到过kali在安装结束后自动重启,屏幕黑屏就显示一个光标. 解决办法: 安装最后一步,不要选择默认项 Enter device manually 改选第二项.....具体什么忘记了. 即可解决 ...

  10. 使用 audioqueue 播放PCM数据

    // // MainViewController.h // RawAudioDataPlayer // // Created by SamYou on 12-8-18. // Copyright (c ...