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. 找到最终的安全状态 在有向图中, 我们从某个节点和每个转向处开始, 沿着图的有向边走. 如果我们到达的节点是终点 (即它没有连出的有向边), 我们停止. 现在, 如果我们最后能走到终点,那么 ...
随机推荐
- 嵌入式02 STM32 实验03 时钟系统
时钟系统是处理器的核心,所以认真学习时钟系统是必要的,有助于深入理解STM32. 由于STM32的外设很多,有的外设不需要太高的时钟频率,同一个电路,时钟越快功耗越大,同时抗电磁干扰能力也越弱,所以对 ...
- python基础教程_查询价格
少儿编程和数学是有这一定联系的,若是将编程弄好了,数学成绩也就会有所提高,今天就为大家来介绍一下一个关于数学的相关编程,现在我们就来看看吧! 题目描述 编程实现以下功能:查询水果的单价.有4种水果,苹 ...
- 全能中间件v19.5.7 正式版发布
v19.5.7 更新=========================1.新增 支持更多微信公众号API.2.优化 AccessToken 刷新机制.3.修复 微信公众号“消息加解密方式”为“安全模式 ...
- 给element添加自定义图标
element为我们提供了丰富的好用的组件,图标的样式也很多,但还是有一些常用的图标没有在官方图标库里边,比如说微信.淘宝.支付宝等等.那么如何把我们需要的图标添加到进去呢? 因为element有官方 ...
- 配置多用户SMB挂载
在 system1 通过 SMB 共享目录 /devops ,并满足下列要求: 1.共享名为 devops 2.共享目录 devops 只能 group8.example.com 域中的客户端使用 3 ...
- golang ---常用函数:make
简介 内建函数 make 用来为 slice,map 或 chan 类型分配内存和初始化一个对象(注意:只能用在这三种类型上) slice // 长度为5,容量为10的slice,slice中的元素是 ...
- oracle plsql基本语法
oracle plsql 基本语法 --plsql默认规则:plsql赋值用":=" plsql判断用"=" plsql输入用"&" ...
- vue动态循环出的多个select出现过的变为disabled
<template> <div class="artcle"> <el-form label-width="100px" :mod ...
- Vue学习之webpack中使用vue(十七)
一.包的查找规则: 1.在项目根目录中找有没有 node_modules 的文件夹: 2.在 node_modules 中根据包名,找对应的vue 文件夹: 3.在vue 文件夹中,找 一个叫做 pa ...
- 将html版API文档转换成chm格式的API文档
文章完全转载自: https://blog.csdn.net/u012557538/article/details/42089277 将html版API文档转换成chm格式的API文档并不是一件难事, ...