Class Solution
java.lang.Object
g0801_0900.s0802_find_eventual_safe_states.Solution
802 - Find Eventual Safe States.<p>Medium</p>
<p>There is a directed graph of <code>n</code> nodes with each node labeled from <code>0</code> to <code>n - 1</code>. The graph is represented by a <strong>0-indexed</strong> 2D integer array <code>graph</code> where <code>graph[i]</code> is an integer array of nodes adjacent to node <code>i</code>, meaning there is an edge from node <code>i</code> to each node in <code>graph[i]</code>.</p>
<p>A node is a <strong>terminal node</strong> if there are no outgoing edges. A node is a <strong>safe node</strong> if every possible path starting from that node leads to a <strong>terminal node</strong>.</p>
<p>Return <em>an array containing all the <strong>safe nodes</strong> of the graph</em>. The answer should be sorted in <strong>ascending</strong> order.</p>
<p><strong>Example 1:</strong></p>
<p><img src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/03/17/picture1.png" alt="Illustration of graph" /></p>
<p><strong>Input:</strong> graph = [[1,2],[2,3],[5],[0],[5],[],[]]</p>
<p><strong>Output:</strong> [2,4,5,6]</p>
<p><strong>Explanation:</strong> The given graph is shown above.</p>
<p>Nodes 5 and 6 are terminal nodes as there are no outgoing edges from either of them.</p>
<p>Every path starting at nodes 2, 4, 5, and 6 all lead to either node 5 or 6.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> graph = [[1,2,3,4],[1,2],[3,4],[0,4],[]]</p>
<p><strong>Output:</strong> [4]</p>
<p><strong>Explanation:</strong> Only node 4 is a terminal node, and every path starting at node 4 leads to node 4.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == graph.length</code></li>
<li><code>1 <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= graph[i].length <= n</code></li>
<li><code>0 <= graph[i][j] <= n - 1</code></li>
<li><code>graph[i]</code> is sorted in a strictly increasing order.</li>
<li>The graph may contain self-loops.</li>
<li>The number of edges in the graph will be in the range <code>[1, 4 * 10<sup>4</sup>]</code>.</li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
eventualSafeNodes
-