802. Find Eventual Safe States Medium

@problem@discussion
#Depth-First Search#Breadth-First Search#Graph#Topological Sort



1/**
2 * [802] Find Eventual Safe States
3 *
4 * There is a directed graph of n nodes with each node labeled from 0 to n - 1. The graph is represented by a 0-indexed 2D integer array graph where graph[i] is an integer array of nodes adjacent to node i, meaning there is an edge from node i to each node in graph[i].
5 * A node is a terminal node if there are no outgoing edges. A node is a safe node if every possible path starting from that node leads to a terminal node (or another safe node).
6 * Return an array containing all the safe nodes of the graph. The answer should be sorted in ascending order.
7 *  
8 * Example 1:
9 * <img alt="Illustration of graph" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/03/17/picture1.png" style="height: 171px; width: 600px;" />
10 * Input: graph = [[1,2],[2,3],[5],[0],[5],[],[]]
11 * Output: [2,4,5,6]
12 * Explanation: The given graph is shown above.
13 * Nodes 5 and 6 are terminal nodes as there are no outgoing edges from either of them.
14 * Every path starting at nodes 2, 4, 5, and 6 all lead to either node 5 or 6.
15 * Example 2:
16 * 
17 * Input: graph = [[1,2,3,4],[1,2],[3,4],[0,4],[]]
18 * Output: [4]
19 * Explanation:
20 * Only node 4 is a terminal node, and every path starting at node 4 leads to node 4.
21 * 
22 *  
23 * Constraints:
24 * 
25 * 	n == graph.length
26 * 	1 <= n <= 10^4
27 * 	0 <= graph[i].length <= n
28 * 	0 <= graph[i][j] <= n - 1
29 * 	graph[i] is sorted in a strictly increasing order.
30 * 	The graph may contain self-loops.
31 * 	The number of edges in the graph will be in the range [1, 4 * 10^4].
32 * 
33 */
34pub struct Solution {}
35
36// problem: https://leetcode.com/problems/find-eventual-safe-states/
37// discuss: https://leetcode.com/problems/find-eventual-safe-states/discuss/?currentPage=1&orderBy=most_votes&query=
38
39// submission codes start here
40
41impl Solution {
42    pub fn eventual_safe_nodes(graph: Vec<Vec<i32>>) -> Vec<i32> {
43        vec![]
44    }
45}
46
47// submission codes end
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn test_802() {
55    }
56}
57


Back
© 2025 bowen.ge All Rights Reserved.