热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

[LeetCode]802.FindEventualSafeStates

Westartatsomenodeinadirectedgraph,andeveryturn,wewalkalongadirectededgeofthegraph.Ifwereac

We start at some node in a directed graph, and every turn, we walk along a directed edge of the graph. If we reach a terminal node (that is, it has no outgoing directed edges), we stop.

We define a starting node to be safe if we must eventually walk to a terminal node. More specifically, there is a natural number k, so that we must have stopped at a terminal node in less than k steps for any choice of where to walk.

Return an array containing all the safe nodes of the graph. The answer should be sorted in ascending order.

The directed graph has n nodes with labels from 0 to 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, going from node i to node j.

 

Example 1:

技术分享图片

Input: graph = [[1,2],[2,3],[5],[0],[5],[],[]]
Output: [2,4,5,6]
Explanation: The given graph is shown above.

Example 2:

Input: graph = [[1,2,3,4],[1,2],[3,4],[0,4],[]]
Output: [4]

Constraints:



  • n == graph.length

  • 1 <= n <= 104

  • 0 <= graph[i].length <= n

  • graph[i] is sorted in a strictly increasing order.

  • The graph may contain self-loops.

  • The number of edges in the graph will be in the range [1, 4 * 104].

找到最终的安全状态。


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

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

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

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

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/find-eventual-safe-states
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。


这是一道图论的题,这里我提供一个DFS的做法。题目说了半天,精简下来其实题目问的是如果你从图中的每个节点都试图出发,是否有可能在走了若干步之后在某个点就停下了,即停下的点是没有 next 节点的。题意反过来理解,就是试图找图中从每个点出发是否存在环,把没有环的起点按照升序排列输出即可。

找环这里我采用的是染色法,建立一个和 input 数组等长的 color 数组,里面代表的是每个节点的颜色情况。一开始初始化为 0,说明节点还未被访问过。用 DFS 开始遍历,一开始把当前点标记为 2,意思是这个点有可能最后是环的一部分。接着往下递归遍历的时候,如果返回的结果是 false 则说明这一溜下去的点都在环上,就都需要被标记成 2;反之如果递归的结果不是 false,则说明从当前点开始是可以走到某个终点的,我们再把当前点的颜色变为 1。

时间O(n)

空间O(n)

Java实现


1 class Solution {
2 public List eventualSafeNodes(int[][] graph) {
3 List res = new ArrayList<>();
4 // corner case
5 if (graph == null || graph.length == 0) {
6 return res;
7 }
8
9 // normal case
10 int count = graph.length;
11 int[] color = new int[count];
12 for (int i = 0; i ) {
13 if (dfs(graph, i, color)) {
14 res.add(i);
15 }
16 }
17 return res;
18 }
19
20 private boolean dfs(int[][] graph, int start, int[] color) {
21 if (color[start] != 0) {
22 return color[start] == 1;
23 }
24 color[start] = 2;
25 for (int next : graph[start]) {
26 if (!dfs(graph, next, color)) {
27 return false;
28 }
29 }
30 color[start] = 1;
31 return true;
32 }
33 }

 

LeetCode 题目总结


推荐阅读
  • 本文探讨了如何通过最小生成树(MST)来计算严格次小生成树。在处理过程中,需特别注意所有边权重相等的情况,以避免错误。我们首先构建最小生成树,然后枚举每条非树边,检查其是否能形成更优的次小生成树。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • This guide provides a comprehensive step-by-step approach to successfully installing the MongoDB PHP driver on XAMPP for macOS, ensuring a smooth and efficient setup process. ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 本文详细介绍了 Dockerfile 的编写方法及其在网络配置中的应用,涵盖基础指令、镜像构建与发布流程,并深入探讨了 Docker 的默认网络、容器互联及自定义网络的实现。 ... [详细]
  • 在Linux系统中配置并启动ActiveMQ
    本文详细介绍了如何在Linux环境中安装和配置ActiveMQ,包括端口开放及防火墙设置。通过本文,您可以掌握完整的ActiveMQ部署流程,确保其在网络环境中正常运行。 ... [详细]
  • 解决VSCode中文乱码问题的综合方案
    在使用VSCode进行开发时,尤其是涉及Python编程,可能会遇到中文乱码的问题。本文总结了多种有效的解决方案,帮助开发者快速解决这一常见问题。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 本文介绍了如何使用jQuery根据元素的类型(如复选框)和标签名(如段落)来获取DOM对象。这有助于更高效地操作网页中的特定元素。 ... [详细]
  • 深入理解Cookie与Session会话管理
    本文详细介绍了如何通过HTTP响应和请求处理浏览器的Cookie信息,以及如何创建、设置和管理Cookie。同时探讨了会话跟踪技术中的Session机制,解释其原理及应用场景。 ... [详细]
  • 本文介绍如何在 Xcode 中使用快捷键和菜单命令对多行代码进行缩进,包括右缩进和左缩进的具体操作方法。 ... [详细]
  • c# – UWP:BrightnessOverride StartOverride逻辑 ... [详细]
  • 解决Linux系统中pygraphviz安装问题
    本文探讨了在Linux环境下安装pygraphviz时遇到的常见问题,并提供了详细的解决方案和最佳实践。 ... [详细]
  • 本文介绍了一款用于自动化部署 Linux 服务的 Bash 脚本。该脚本不仅涵盖了基本的文件复制和目录创建,还处理了系统服务的配置和启动,确保在多种 Linux 发行版上都能顺利运行。 ... [详细]
  • CMake跨平台开发实践
    本文介绍如何使用CMake支持不同平台的代码编译。通过一个简单的示例,我们将展示如何编写CMakeLists.txt以适应Linux和Windows平台,并实现跨平台的函数调用。 ... [详细]
author-avatar
whglwz
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有