当前位置:网站首页>Simulation volume leetcode [general] 1557 The minimum number of points that can reach all points
Simulation volume leetcode [general] 1557 The minimum number of points that can reach all points
2022-07-07 08:55:00 【Encounter simulation volume】
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 .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/minimum-number-of-vertices-to-reach-all-nodes
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Code :
from leetcode_python.utils import *
class Solution:
def __init__(self):
""" Overtime 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_ Overtime (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 turn 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')
remarks :
GitHub:https://github.com/monijuan/leetcode_python
CSDN Summary : Simulation volume Leetcode Summary of questions _ Paper blog -CSDN Blog
You can add QQ Group communication :1092754609
leetcode_python.utils See the description on the summary page for details
First brush questions , Then generated by script blog, If there is any mistake, please leave a message , I see it will be revised ! thank you !
边栏推荐
猜你喜欢
oracle一次性说清楚,多种分隔符的一个字段拆分多行,再多行多列多种分隔符拆多行,最终处理超亿亿。。亿级别数据量
2022-06-30 Unity核心8——模型导入
A bug using module project in idea
Pointer advanced, string function
UnityShader入门精要个人总结--基础篇(一)
指针进阶,字符串函数
面试题:高速PCB一般布局、布线原则
Lenovo hybrid cloud Lenovo xcloud: 4 major product lines +it service portal
Mountaineering team (DFS)
Data analysis methodology and previous experience summary 2 [notes dry goods]
随机推荐
FPGA knowledge accumulation [6]
Golang etcdv3 reports an error. The attribute in grpc does not exist
QT charts use (rewrite qchartview to realize some custom functions)
Test pits - what test points should be paid attention to when adding fields to existing interfaces (or database tables)?
OpenGL帧缓冲
【ChaosBlade:节点 CPU 负载、节点网络延迟、节点网络丢包、节点域名访问异常】
【踩坑】nacos注册一直连接localhost:8848,no available server
Greenplum6.x-版本变化记录-常用手册
NCS Chengdu Xindian interview experience
Simple use of Xray
年薪50w阿裏P8親自下場,教你如何從測試進階
如何在图片的目标中添加目标的mask
【Istio Network CRD VirtualService、Envoyfilter】
Greenplum 6.x monitoring software setup
Isomorphic C language
Led analog and digital dimming
Category of IP address
硬件大熊原创合集(2022/05更新)
Upload an e-office V9 arbitrary file [vulnerability recurrence practice]
Explain Huawei's application market in detail, and gradually reduce 32-bit package applications and strategies in 2022