当前位置:网站首页>模拟卷Leetcode【普通】1557. 可以到达所有点的最少点数目
模拟卷Leetcode【普通】1557. 可以到达所有点的最少点数目
2022-07-07 06:18:00 【邂逅模拟卷】
1557. 可以到达所有点的最少点数目
给你一个 有向无环图 , n 个节点编号为 0 到 n-1 ,以及一个边数组 edges ,其中 edges[i] = [fromi, toi] 表示一条从点 fromi 到点 toi 的有向边。
找到最小的点集使得从这些点出发能到达图中所有点。题目保证解存在且唯一。
你可以以任意顺序返回这些节点编号。
示例 1:
输入:n = 6, edges = [[0,1],[0,2],[2,5],[3,4],[4,2]]
输出:[0,3]
解释:从单个节点出发无法到达所有节点。从 0 出发我们可以到达 [0,1,2,5] 。从 3 出发我们可以到达 [3,4,2,5] 。所以我们输出 [0,3] 。
示例 2:
输入:n = 5, edges = [[0,1],[2,1],[3,1],[1,4],[2,4]]
输出:[0,2,3]
解释:注意到节点 0,3 和 2 无法从其他节点到达,所以我们必须将它们包含在结果点集中,这些点都能到达节点 1 和 4 。
提示:
2 <= n <= 10^5
1 <= edges.length <= min(10^5, n * (n - 1) / 2)
edges[i].length == 2
0 <= fromi, toi < n
所有点对 (fromi, toi) 互不相同。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/minimum-number-of-vertices-to-reach-all-nodes
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码:
from leetcode_python.utils import *
class Solution:
def __init__(self):
""" 超时case:https://leetcode-cn.com/submissions/detail/240988246/testcase/ """
pass
def findSmallestSetOfVertices(self, n: int, edges: List[List[int]]) -> List[int]:
res = set(range(n))
for start,end in edges:
if end in res: res.remove(end)
return list(res)
def findSmallestSetOfVertices_超时(self, n: int, edges: List[List[int]]) -> List[int]:
res = [x for x in range(n)]
for start,end in edges:
if end in res: res.remove(end)
return res
def test(data_test):
s = Solution()
data = data_test # normal
# data = [list2node(data_test[0])] # list转node
return s.findSmallestSetOfVertices(*data)
def test_obj(data_test):
result = [None]
obj = Solution(*data_test[1][0])
for fun, data in zip(data_test[0][1::], data_test[1][1::]):
if data:
res = obj.__getattribute__(fun)(*data)
else:
res = obj.__getattribute__(fun)()
result.append(res)
return result
if __name__ == '__main__':
datas = [
[6,[[0,1],[0,2],[2,5],[3,4],[4,2]]],
]
for data_test in datas:
t0 = time.time()
print('-' * 50)
print('input:', data_test)
print('output:', test(data_test))
print(f'use time:{
time.time() - t0}s')
备注:
GitHub:https://github.com/monijuan/leetcode_python
CSDN汇总:模拟卷Leetcode 题解汇总_卷子的博客-CSDN博客
可以加QQ群交流:1092754609
leetcode_python.utils详见汇总页说明
先刷的题,之后用脚本生成的blog,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- Required String parameter ‘XXX‘ is not present
- 【踩坑】nacos注册一直连接localhost:8848,no available server
- selenium自动化集成,八年测试经验软测工程师,一篇文章带你学懂
- Laravel8 uses passport login and JWT (generate token)
- 23 Chengdu instrument customization undertaking_ Discussion on automatic wiring method of PCB in Protel DXP
- Greenplum 6.x reinitialization
- Analysis of using jsonp cross domain vulnerability and XSS vulnerability in honeypot
- Go write a program that runs within a certain period of time
- Frequently Asked Coding Problems
- Quick sorting (detailed illustration of single way, double way, three way)
猜你喜欢
![[Nanjing University] - [software analysis] course learning notes (I) -introduction](/img/57/bf652b36389d2bf95388d2eb4772a1.png)
[Nanjing University] - [software analysis] course learning notes (I) -introduction

IP地址的类别

Greenplum 6.x build_ Environment configuration

Appeler l'interface du moteur de création du service multimédia de jeu Huawei renvoie le Code d'erreur 1002, le message d'erreur: les paramètres sont l'erreur

Mountaineering team (DFS)

Greenplum6.x重新初始化

【MySQL】数据库进阶之触发器内容详解

LeetCode 715. Range 模块

21 general principles of wiring in circuit board design_ Provided by Chengdu circuit board design

PPT模板、素材下载网站(纯干货,建议收藏)
随机推荐
Analysis of using jsonp cross domain vulnerability and XSS vulnerability in honeypot
为什么要选择云原生数据库
leetcode134. gas station
Frequently Asked Coding Problems
Speaking of a software entrepreneurship project, is there anyone willing to invest?
如何在HarmonyOS应用中集成App Linking服务
数据分析方法论与前人经验总结2【笔记干货】
let const
关于基于kangle和EP面板使用CDN
Tronapi wave field interface - source code without encryption - can be opened twice - interface document attached - package based on thinkphp5 - detailed guidance of the author - July 6, 2022 - Novice
[Yu Yue education] higher vocational English reference materials of Nanjing Polytechnic University
Leetcode 1984. Minimum difference in student scores
Greenplum 6.x common statements
ESP32-ULP协处理器低功耗模式RTC GPIO中断唤醒
[Chongqing Guangdong education] accounting reference materials of Nanjing University of Information Engineering
Tronapi-波场接口-源码无加密-可二开--附接口文档-基于ThinkPHP5封装-作者详细指导-2022年7月6日-新手快速上手-可无缝升级tp6版本
What are the advantages of commas in conditional statements- What is the advantage of commas in a conditional statement?
NCS Chengdu Xindian interview experience
Uniapp wechat applet monitoring network
数字三角形模型 AcWing 1027. 方格取数