802:找到最终的安全状态

This commit is contained in:
huangge1199 2021-08-05 16:42:05 +08:00
parent 3fb6cb7345
commit d8832d3cac
3 changed files with 146 additions and 3 deletions

View File

@ -0,0 +1,103 @@
//在有向图中以某个节点为起始节点从该点出发每一步沿着图中的一条有向边行走如果到达的节点是终点即它没有连出的有向边则停止
//
// 对于一个起始节点如果从该节点出发无论每一步选择沿哪条有向边行走最后必然在有限步内到达终点则将该起始节点称作是 安全
//
// 返回一个由图中所有安全的起始节点组成的数组作为答案答案数组中的元素应当按 升序 排列
//
// 该有向图有 n 个节点 0 n - 1 编号其中 n graph 的节点数图以下述形式给出graph[i] 是编号 j 节点的一个列表
//满足 (i, j) 是图的一条有向边
//
//
//
//
//
// 示例 1
//
//
//输入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]
//
//
//
// Related Topics 深度优先搜索 广度优先搜索 拓扑排序
// 👍 163 👎 0
package leetcode.editor.cn;
import com.code.leet.entiy.TwoArray;
import java.util.*;
//802:找到最终的安全状态
class FindEventualSafeStates {
public static void main(String[] args) {
//测试代码
Solution solution = new FindEventualSafeStates().new Solution();
TwoArray array = new TwoArray("[[1,2],[2,3],[5],[0],[5],[],[]]", false);
System.out.println(solution.eventualSafeNodes(array.getArr()));
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public List<Integer> eventualSafeNodes(int[][] graph) {
Map<Integer, List<Integer>> map = new HashMap<>();
int[] count = new int[graph.length];
for (int i = 0; i < graph.length; i++) {
for (int j = 0; j < graph[i].length; j++) {
List<Integer> list = map.getOrDefault(graph[i][j], new ArrayList<>());
list.add(i);
map.put(graph[i][j], list);
}
count[i] = graph[i].length;
}
Queue<Integer> queue = new LinkedList<>();
for (int i = 0; i < graph.length; i++) {
if (count[i] == 0) {
queue.add(i);
}
}
List<Integer> list = new ArrayList<>();
while (!queue.isEmpty()) {
int num = queue.poll();
list.add(num);
if (map.containsKey(num)) {
for (Integer integer : map.get(num)) {
if (list.contains(integer)) {
continue;
}
count[integer]--;
if (count[integer] == 0) {
queue.add(integer);
}
}
}
}
Collections.sort(list);
return list;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,42 @@
<p>在有向图中,以某个节点为起始节点,从该点出发,每一步沿着图中的一条有向边行走。如果到达的节点是终点(即它没有连出的有向边),则停止。</p>
<p>对于一个起始节点,如果从该节点出发,<strong>无论每一步选择沿哪条有向边行走</strong>,最后必然在有限步内到达终点,则将该起始节点称作是 <strong>安全</strong> 的。</p>
<p>返回一个由图中所有安全的起始节点组成的数组作为答案。答案数组中的元素应当按 <strong>升序</strong> 排列。</p>
<p>该有向图有 <code>n</code> 个节点,按 <code>0</code><code>n - 1</code> 编号,其中 <code>n</code>&nbsp;<code>graph</code>&nbsp;的节点数。图以下述形式给出:<code>graph[i]</code> 是编号 <code>j</code> 节点的一个列表,满足 <code>(i, j)</code> 是图的一条有向边。</p>
<p>&nbsp;</p>
<div class="original__bRMd">
<div>
<p><strong>示例 1</strong></p>
<img alt="Illustration of graph" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/03/17/picture1.png" style="height: 171px; width: 600px;" />
<pre>
<strong>输入:</strong>graph = [[1,2],[2,3],[5],[0],[5],[],[]]
<strong>输出:</strong>[2,4,5,6]
<strong>解释:</strong>示意图如上。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>graph = [[1,2,3,4],[1,2],[3,4],[0,4],[]]
<strong>输出:</strong>[4]
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>n == graph.length</code></li>
<li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li>
<li><code>0 &lt;= graph[i].length &lt;= n</code></li>
<li><code>graph[i]</code> 按严格递增顺序排列。</li>
<li>图中可能包含自环。</li>
<li>图中边的数目在范围 <code>[1, 4 * 10<sup>4</sup>]</code> 内。</li>
</ul>
</div>
</div>
<div><div>Related Topics</div><div><li>深度优先搜索</li><li>广度优先搜索</li><li></li><li>拓扑排序</li></div></div>\n<div><li>👍 163</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long