当前位置:网站首页>模拟卷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,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- Rapid integration of authentication services - harmonyos platform
- LeetCode 715. Range 模块
- 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
- IP地址的类别
- Required String parameter ‘XXX‘ is not present
- Golan idea IntelliJ cannot input Chinese characters
- Implementation of navigation bar at the bottom of applet
- PPT模板、素材下载网站(纯干货,建议收藏)
- 阿里p8推荐,测试覆盖率工具—Jacoco,实用性极佳
- 测试人一定要会的技能:selenium的三种等待方式解读,清晰明了
猜你喜欢
最长上升子序列模型 AcWing 1017. 怪盗基德的滑翔翼
Data analysis methodology and previous experience summary 2 [notes dry goods]
Arm GIC (IV) GIC V3 register class analysis notes.
Markdown编辑器Editor.md插件的使用
指针进阶,字符串函数
IP地址的类别
Lenovo hybrid cloud Lenovo xcloud: 4 major product lines +it service portal
Pointer advanced, string function
ncs成都新電面試經驗
ncs成都新电面试经验
随机推荐
Implement custom memory allocator
ncs成都新电面试经验
String operation
[Chongqing Guangdong education] accounting reference materials of Nanjing University of Information Engineering
[Nanjing University] - [software analysis] course learning notes (I) -introduction
Componentspace2022, assertions, protocols, bindings, and configuration files
如何统计项目代码行数
leetcode134. gas station
快速集成认证服务-HarmonyOS平台
ESP32-ULP协处理器低功耗模式RTC GPIO中断唤醒
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
Tronapi-波场接口-源码无加密-可二开--附接口文档-基于ThinkPHP5封装-作者详细指导-2022年7月6日-新手快速上手-可无缝升级tp6版本
Mountaineering team (DFS)
let const
为不同类型设备构建应用的三大更新 | 2022 I/O 重点回顾
Uniapp wechat applet monitoring network
PPT模板、素材下载网站(纯干货,建议收藏)
Opencv converts 16 bit image data to 8 bits and 8 to 16
平台化,强链补链的一个支点
联想混合云Lenovo xCloud:4大产品线+IT服务门户