当前位置:网站首页>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^5
1 <= edges.length <= min(10^5, n * (n - 1) / 2)
edges[i].length == 2
0 <= 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.
边栏推荐
- Solve the single thread scheduling problem of intel12 generation core CPU (II)
- Effet d'utilisation, déclenché lorsque les composants de la fonction sont montés et déchargés
- 业务系统兼容数据库Oracle/PostgreSQL(openGauss)/MySQL的琐事
- Simple records of business system migration from Oracle to opengauss database
- 软通乐学-js求字符串中字符串当中那个字符出现的次数多 -冯浩的博客
- 业务系统从Oracle迁移到openGauss数据库的简单记录
- Chapter 5 detailed explanation of consumer groups
- Market trend report, technological innovation and market forecast of double door and multi door refrigerators in China
- Audio and video development interview questions
- Problem - 1646C. Factorials and Powers of Two - Codeforces
猜你喜欢
QT implementation fillet window
Local visualization tools are connected to redis of Alibaba cloud CentOS server
Audio and video development interview questions
使用jq实现全选 反选 和全不选-冯浩的博客
Browser print margin, default / borderless, full 1 page A4
Pull branch failed, fatal: 'origin/xxx' is not a commit and a branch 'xxx' cannot be created from it
Chapter 5 yarn resource scheduler
Story of [Kun Jintong]: talk about Chinese character coding and common character sets
第五章 Yarn资源调度器
Native JS realizes the functions of all selection and inverse selection -- Feng Hao's blog
随机推荐
Codeforces Round #771 (Div. 2)
力扣leetcode第 280 场周赛
Market trend report, technical innovation and market forecast of double-sided foam tape in China
Codeforces Round #801 (Div. 2)A~C
Problem - 922D、Robot Vacuum Cleaner - Codeforces
MariaDB的安装与配置
Market trend report, technical innovation and market forecast of China's desktop capacitance meter
Browser print margin, default / borderless, full 1 page A4
Codeforces Round #802(Div. 2)A~D
Simply try the new amp model of deepfacelab (deepfake)
Acwing: the 56th weekly match
Classic application of stack -- bracket matching problem
Spark独立集群动态上线下线Worker节点
Installation and configuration of MariaDB
第6章 DataNode
Raspberry pie 4b64 bit system installation miniconda (it took a few days to finally solve it)
第6章 Rebalance详解
本地可视化工具连接阿里云centOS服务器的redis
Remove the border when input is focused
FLV格式详解