1. 程式人生 > 其它 >[LeetCode] 802. Find Eventual Safe States

[LeetCode] 802. Find Eventual Safe States

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 besafeif we must eventually walk to a terminal node. More specifically, there is a natural numberk

, so that we must have stopped at a terminal node in less thanksteps forany choice of where to walk.

Returnan array containing all the safe nodes of the graph. The answer should be sorted inascendingorder.

The directed graph hasnnodes with labels from0ton - 1, wherenis the length ofgraph. The graph is given in the following form:graph[i]

is a list of labelsjsuch that(i, j)is a directed edge of the graph, going from nodeito nodej.

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<Integer> eventualSafeNodes(int[][] graph) {
 3         List<Integer> 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 < count; 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 題目總結