当前位置:网站首页>模拟卷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,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- Tronapi-波场接口-源码无加密-可二开--附接口文档-基于ThinkPHP5封装-作者详细指导-2022年7月6日-新手快速上手-可无缝升级tp6版本
- 登山小分队(dfs)
- [wechat applet: cache operation]
- Markdown编辑器Editor.md插件的使用
- Sign and authenticate API interface or H5 interface
- Un salaire annuel de 50 W Ali P8 vous montrera comment passer du test
- Implement custom memory allocator
- NCS Chengdu Xindian interview experience
- Required String parameter ‘XXX‘ is not present
- What are the advantages of commas in conditional statements- What is the advantage of commas in a conditional statement?
猜你喜欢
【踩坑】nacos注册一直连接localhost:8848,no available server
Routing information protocol rip
LeetCode 715. Range module
为不同类型设备构建应用的三大更新 | 2022 I/O 重点回顾
Data type - floating point (C language)
[Nanjing University] - [software analysis] course learning notes (I) -introduction
[Yugong series] February 2022 U3D full stack class 005 unity engine view
Componentspace2022, assertions, protocols, bindings, and configuration files
NCS Chengdu Xindian interview experience
Analysis of using jsonp cross domain vulnerability and XSS vulnerability in honeypot
随机推荐
Greenplum 6.x build_ install
Test pits - what test points should be paid attention to when adding fields to existing interfaces (or database tables)?
Category of IP address
[南京大学]-[软件分析]课程学习笔记(一)-introduction
注解@ConfigurationProperties的三种使用场景
Esp32-ulp coprocessor low power mode RTC GPIO interrupt wake up
[step on the pit] Nacos registration has been connected to localhost:8848, no available server
路由信息协议——RIP
为什么要选择云原生数据库
Greenplum 6.x version change record common manual
阿里p8手把手教你,自动化测试应该如何实现多线程?赶紧码住
测试人一定要会的技能:selenium的三种等待方式解读,清晰明了
調用華為遊戲多媒體服務的創建引擎接口返回錯誤碼1002,錯誤信息:the params is error
Greenplum 6.x reinitialization
[Nanjing University] - [software analysis] course learning notes (I) -introduction
Basic data types and string types are converted to each other
Implementation of navigation bar at the bottom of applet
如何在图片的目标中添加目标的mask
年薪50w阿裏P8親自下場,教你如何從測試進階
GoLand set goproxy