当前位置:网站首页>LeetCode 1557. The minimum number of points that can reach all points
LeetCode 1557. The minimum number of points that can reach all points
2022-07-06 16:43:00 【Daylight629】
1557. The minimum number of points that can reach all points
To give you one Directed acyclic graph , n Node number is 0 To n-1 , And an array of edges edges , among edges[i] = [fromi, toi] Represents a slave point fromi point-to-point toi The directed side of .
Find the smallest set of points so that all points in the graph can be reached from these points . The problem guarantees that the solution exists and is unique .
You can return these node numbers in any order .
Example 1:

Input :n = 6, edges = [[0,1],[0,2],[2,5],[3,4],[4,2]]
Output :[0,3]
explain : All nodes cannot be reached from a single node . from 0 We can get to [0,1,2,5] . from 3 We can get to [3,4,2,5] . So we output [0,3] .
Example 2:

Input :n = 5, edges = [[0,1],[2,1],[3,1],[1,4],[2,4]]
Output :[0,2,3]
explain : Note the node 0,3 and 2 Unable to reach... From other nodes , So we have to include them in the result point set , These points can reach the node 1 and 4 .
Tips :
2 <= n <= 10^51 <= edges.length <= min(10^5, n * (n - 1) / 2)edges[i].length == 20 <= fromi, toi < n- All point pairs
(fromi, toi)Different from each other .
Two 、 Method 1
Looking for penetration 0 The node of is
class Solution {
public List<Integer> findSmallestSetOfVertices(int n, List<List<Integer>> edges) {
List<Integer> res = new ArrayList<>();
Set<Integer> set = new HashSet<>();
for (List<Integer> edge : edges) {
set.add(edge.get(1));
}
for (int i = 0; i < n; i++) {
if (!set.contains(i)) {
res.add(i);
}
}
return res;
}
}
Complexity analysis
Time complexity :O(m+n), among m Is the number of edges in the graph ,n Is the number of nodes in the graph . You need to traverse all the edges to get nodes with non-zero penetration , Then traverse all nodes and keep the nodes with zero penetration .
Spatial complexity :O(n), among n Is the number of nodes in the graph . You need to use a collection to store nodes with non-zero penetration , The number of nodes in the collection will not exceed n.
边栏推荐
- sublime text 代码格式化操作
- 解决Intel12代酷睿CPU单线程只给小核运行的问题
- 图像处理一百题(1-10)
- China double brightening film (dbef) market trend report, technical dynamic innovation and market forecast
- Solve the problem of intel12 generation core CPU [small core full, large core onlookers] (win11)
- Li Kou - 298th weekly match
- SQL快速入门
- Research Report on hearing health care equipment industry - market status analysis and development prospect prediction
- Audio and video development interview questions
- How to insert mathematical formulas in CSDN blog
猜你喜欢

Li Kou - 298th weekly match

Spark独立集群动态上线下线Worker节点

JS encapsulates the method of array inversion -- Feng Hao's blog

Summary of game theory

ffmpeg命令行使用

QT style settings of qcobobox controls (rounded corners, drop-down boxes, up expansion, editable, internal layout, etc.)

Cmake Express

第7章 __consumer_offsets topic

Two weeks' experience of intermediate software designer in the crash soft exam

Tree of life (tree DP)
随机推荐
CMake Error: Could not create named generator Visual Studio 16 2019解决方法
Discussion on QWidget code setting style sheet
Ffmpeg command line use
Spark独立集群Worker和Executor的概念
Solve the problem that intel12 generation core CPU single thread only runs on small cores
Audio and video development interview questions
sublime text 代码格式化操作
第五章 Yarn资源调度器
Spark独立集群动态上线下线Worker节点
Codeforces - 1526C1&&C2 - Potions
Click QT button to switch qlineedit focus (including code)
指定格式时间,月份天数前补零
Two weeks' experience of intermediate software designer in the crash soft exam
QT realizes window topping, topping state switching, and multi window topping priority relationship
Research Report on market supply and demand and strategy of China's four seasons tent industry
业务系统从Oracle迁移到openGauss数据库的简单记录
简单尝试DeepFaceLab(DeepFake)的新AMP模型
Research Report on market supply and demand and strategy of China's tetraacetylethylenediamine (TAED) industry
(lightoj - 1323) billiard balls (thinking)
Li Kou - 298th weekly match