802. 找到最终的安全状态

在有向图中,以某个节点为起始节点,从该点出发,每一步沿着图中的一条有向边行走。如果到达的节点是终点(即它没有连出的有向边),则停止。

对于一个起始节点,如果从该节点出发,无论每一步选择沿哪条有向边行走,最后必然在有限步内到达终点,则将该起始节点称作是 安全 的。

返回一个由图中所有安全的起始节点组成的数组作为答案。答案数组中的元素应当按 升序 排列。

该有向图有 n 个节点,按 0 到 n - 1 编号,其中 n 是 graph 的节点数。图以下述形式给出:graph[i] 是编号 j 节点的一个列表,满足 (i, j) 是图的一条有向边。

  • 示例 1:

image.png

输入:graph = [[1,2],[2,3],[5],[0],[5],[],[]]
输出:[2,4,5,6]
解释:示意图如上。

  • 示例 2:

输入:graph = [[1,2,3,4],[1,2],[3,4],[0,4],[]]
输出:[4]

提示:

n == graph.length
1 <= n <= 104
0 <= graph[i].length <= n
graph[i] 按严格递增顺序排列。
图中可能包含自环。
图中边的数目在范围 [1, 4 * 104] 内。

解题思路

这题原来的路径是从起点经过所以的可能路径都能到达终点,也就是说我们路径上不能产生环,我们可以使用拓扑排序来检查是否有环的产生。

  1. 我们先将图的指向全部变为反向,那么我们的目标就变为从终点去到达起点
  2. map维护一个节点指向其他节点的边,使用一个数组维护每个节点的入度
  3. 当节点的入度为0时,代表该节点的依赖节点已经全部满足,可以将当前节点删除,将其指向的节点全部入度减去1,而对于环,因为他们的节点之间存在循环的依赖,因此永远不可能被删除,所以最后入度为0就是可在有限步内到达终点的节点

代码

class Solution {public List<Integer> eventualSafeNodes(int[][] graph) {int n = graph.length;Queue<Integer> queue=new LinkedList<>();Map<Integer,List<Integer>> map=new HashMap<>();int[] degree=new int[n];for (int i = 0; i < graph.length; i++) {for (int j : graph[i]) {map.putIfAbsent(j,new ArrayList<Integer>());map.get(j).add(i);}degree[i]=graph[i].length;if (degree[i]==0)queue.add(i);}while (!queue.isEmpty()){Integer cur = queue.poll();for (Integer next : map.getOrDefault(cur, new ArrayList<>())) {if (--degree[next]==0)queue.add(next);}}ArrayList<Integer> res = new ArrayList<>();for (int i = 0; i < n; i++) {if (degree[i]==0)res.add(i);}return res;}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/390365.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

元类型与类型的区别

元类型是指所有类型的类型。 元类型只能类型出现在类型标示位&#xff1b; 类型即能作为类型存在&#xff0c;出现在类型标示位&#xff1b; 也能作为变量存在&#xff0c;出现在元类型的变量位。 http://www.swift51.com/swift2.0/chapter3/03_Types.html#type_inheritance_cl…

css 动画使用_如何在CSS中使用动画

css 动画使用使用CSS动画 (Using CSS Animations) CSS animations add beauty to the webpages and make transitions from one CSS style to the other beautiful.CSS动画可以使网页更加美观&#xff0c;并可以从一种CSS样式过渡到另一种CSS样式。 To create a CSS animation…

第01章—快速构建

spring boot 系列学习记录&#xff1a;http://www.cnblogs.com/jinxiaohang/p/8111057.html 码云源码地址&#xff1a;https://gitee.com/jinxiaohang/springboot 一、Spring Initializr 使用教程 &#xff08;IntelliJ IDEA&#xff09; 具体步骤&#xff1a; 1、打开IDEA &am…

看板和scrum_看板VS Scrum-如何变得敏捷

看板和scrumScrum and Kanban are the two most popular project management techniques today in business. As a developer, I think its important to understand these processes as you will likely be heavily involved in them if you are part of a team. By understan…

JS之Promise

开胃菜&#xff0c;先做如下思考&#xff1a; Promise 有几种状态&#xff1f;Promise 状态之间可以转化吗&#xff1f;Promise 中的异常可以被 try...catch 捕获吗&#xff1f;Promise前世 callback hell 大家都知道JS是异步操作&#xff08;执行&#xff09;的&#xff0c;在…

鱼眼镜头的distortion校正【matlab】

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 作者&#xff1a;WWC %%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% 功能&#xff1a;畸变矫正 clc; clear; close all; %% 读取图像 Aimread(D:\文件及下载相关\图片\distortion2.jpg)…

web后端开发学习路线_学习后端Web开发的最佳方法

web后端开发学习路线My previous article described how you can get into frontend development. It also discussed how the front end can be a place filled with landmines – step in the wrong place and youll be overwhelmed by the many frameworks of the JavaScrip…

C# 使用WinApi操作剪切板Clipboard

前言&#xff1a; 最近正好写一个程序&#xff0c;需要操作剪切板 功能很简单&#xff0c;只需要从剪切板内读取字符串&#xff0c;然后清空剪切板&#xff0c;然后再把字符串导入剪切板 我想当然的使用我最拿手的C#来完成这项工作&#xff0c;原因无他&#xff0c;因为.Net框架…

聊聊spring cloud gateway的XForwardedHeadersFilter

序 本文主要研究spring cloud gateway的XForwardedHeadersFilter GatewayAutoConfiguration spring-cloud-gateway-core-2.0.0.RC1-sources.jar!/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java Configuration ConditionalOnProperty(name "sp…

node缓冲区_Node.js缓冲区介绍

node缓冲区什么是缓冲液&#xff1f; (What are Buffers?) Binary is simply a set or a collection of 1 and 0. Each number in a binary, each 1 and 0 in a set are called a bit. Computer converts the data to this binary format to store and perform operations. Fo…

专访赵加雨:WebRTC在网易云信的落地

去年的这个时候&#xff0c;在市面上公开表示使用WebRTC的公司还没几家&#xff0c;但2018年以来&#xff0c;宣布采用或支持WebRTC的公司已经越来越多。实时音视频提供商网易云信也在自研的NRTC中集成了WebRTC。在他们眼里&#xff0c;2017年是WebRTC的转折之年&#xff0c;而…

html/css杂题

1、css选择器&#xff1a;详细&#xff08;http://www.ruanyifeng.com/blog/2009/03/css_selectors.html&#xff09; 派生选择器&#xff1a;按标签 类别选择器&#xff1a;按class ID选择器&#xff1a;按ID 通用选择器&#xff1a;* 匹配所有 属性选择器&#xff1a;按属性&…

黑客马拉松 招募_我如何赢得第一次黑客马拉松-研究,设计和编码的2个狂野日子

黑客马拉松 招募I had no coding or engineering background. I studied biology in college, with no clue about what to do with my degree. 我没有编码或工程背景。 我在大学学习生物学&#xff0c;但不知道如何处理我的学位。 My first jobs were making cold calls in s…

1、Linux命令随笔

1 Linux命令总结2 3 man 命令帮助;4 help 命令的帮助&#xff08;bash的内置命令&#xff09;;5 ls list,查看目录列表;6 -ld&#xff1a;查看目录权限;7 -l:(long)长格式显示属性;8 -F:给不同的文件类型结尾加标识9 -p:给目录加斜线10 …

1137. 第 N 个泰波那契数

泰波那契序列 Tn 定义如下&#xff1a; T0 0, T1 1, T2 1, 且在 n > 0 的条件下 Tn3 Tn Tn1 Tn2 给你整数 n&#xff0c;请返回第 n 个泰波那契数 Tn 的值。 示例 1&#xff1a; 输入&#xff1a;n 4 输出&#xff1a;4 解释&#xff1a; T_3 0 1 1 2 T_4 1…

web图像_Web图像优化的基本介绍

web图像Images are an essential ingredient of most websites. The visual quality of pictures has a direct impact on the brand image and the message those images convey. And the weight of images usually accounts for a 40-60% of the data transferred on the web…

ElasticSearch客户端注解使用介绍

The best elasticsearch highlevel java rest api-----bboss 1.ElasticSearch客户端bboss提供了一系列注解 ESId 用于标识实体对象中作为docid的属性&#xff0c;该注解只有一个persistent 布尔值属性&#xff0c;用于控制被本注解标注的字段属性是否作为普通文档属性保存&am…

5827. 检查操作是否合法

给你一个下标从 0 开始的 8 x 8 网格 board &#xff0c;其中 board[r][c] 表示游戏棋盘上的格子 (r, c) 。棋盘上空格用 ‘.’ 表示&#xff0c;白色格子用 ‘W’ 表示&#xff0c;黑色格子用 ‘B’ 表示。 游戏中每次操作步骤为&#xff1a;选择一个空格子&#xff0c;将它变…

团队的远程管理_远程团队指南:如何管理您的远程软件开发团队

团队的远程管理Guides to help you work remotely seem to have swept through the Internet these days. 这些天来&#xff0c;帮助您远程工作的指南似乎席卷了Internet。 Do this, avoid that, stay productive, and all those run-of-the-mill tips we’ve already tried o…

JS 正则 钱

function ValidateIsDecial(sValue) {return (!sValue && !!!sValue && /^[0-9]{1,10}(\.[0-9]{0,2})?$/.test(sValue)); };验证 decimal(12,2) 小数点前允许10位,小数点后允许2位 1234567890 true 12345678901 false 0123456789 true 01234567891 false 123.…