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.pswp.cn/news/390365.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390365.shtml
英文地址,請注明出處:http://en.pswp.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.…