当前位置:网站首页>模拟卷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,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- Componentspace2022, assertions, protocols, bindings, and configuration files
- Greenplum6.x常用语句
- Laravel8 uses passport login and JWT (generate token)
- LeetCode 715. Range 模块
- Compilation and linking of programs
- Golan idea IntelliJ cannot input Chinese characters
- Greenplum 6.x monitoring software setup
- Routing information protocol rip
- About using CDN based on Kangle and EP panel
- Shell script for changing the current folder and the file date under the folder
猜你喜欢
随机推荐
Un salaire annuel de 50 W Ali P8 vous montrera comment passer du test
MAC OSX php dyld: Library not loaded: /usr/local/xxxx. dylib
oracle一次性说清楚,多种分隔符的一个字段拆分多行,再多行多列多种分隔符拆多行,最终处理超亿亿。。亿级别数据量
cmake命令行使用
Upload an e-office V9 arbitrary file [vulnerability recurrence practice]
Analysis of abnormal channel number information before and after AGC re signature service
Data analysis methodology and previous experience summary 2 [notes dry goods]
Sign and authenticate API interface or H5 interface
【微信小程序:缓存操作】
年薪50w阿裏P8親自下場,教你如何從測試進階
How to integrate app linking services in harmonyos applications
Go write a program that runs within a certain period of time
Rapid integration of authentication services - harmonyos platform
Qt Charts使用(重写QChartView,实现一些自定义功能)
数据分析方法论与前人经验总结2【笔记干货】
Novice entry SCM must understand those things
为不同类型设备构建应用的三大更新 | 2022 I/O 重点回顾
QT charts use (rewrite qchartview to realize some custom functions)
数据库存储---表分区
Data type - integer (C language)