当前位置:网站首页>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 !
边栏推荐
- Goldbach conjecture C language
- Analysis of abnormal channel number information before and after AGC re signature service
- About using CDN based on Kangle and EP panel
- Newly found yii2 excel processing plug-in
- 2022-06-30 Unity核心8——模型导入
- Markdown编辑器Editor.md插件的使用
- LeetCode 736. LISP syntax parsing
- OpenGL帧缓冲
- Alibaba P8 teaches you how to realize multithreading in automated testing? Hurry up and stop
- 指针进阶,字符串函数
猜你喜欢

Led analog and digital dimming

Digital triangle model acwing 1027 Grid access

为不同类型设备构建应用的三大更新 | 2022 I/O 重点回顾

LeetCode 715. Range 模块

Output all composite numbers between 6 and 1000

opencv之图像分割

Greenplum 6.x reinitialization

Greenplum6.x-版本变化记录-常用手册

ESP32-ULP协处理器低功耗模式RTC GPIO中断唤醒

LeetCode 736. LISP syntax parsing
随机推荐
Led analog and digital dimming
Un salaire annuel de 50 W Ali P8 vous montrera comment passer du test
数据分析方法论与前人经验总结2【笔记干货】
[Yugong series] February 2022 U3D full stack class 006 unity toolbar
模拟卷Leetcode【普通】1557. 可以到达所有点的最少点数目
Three usage scenarios of annotation @configurationproperties
The longest ascending subsequence model acwing 1017 Strange thief Kidd's glider
Problems encountered in the use of go micro
【ChaosBlade:节点磁盘填充、杀节点上指定进程、挂起节点上指定进程】
模拟卷Leetcode【普通】1567. 乘积为正数的最长子数组长度
面试题:高速PCB一般布局、布线原则
IP地址的类别
ESP32-ULP协处理器低功耗模式RTC GPIO中断唤醒
What are the advantages of commas in conditional statements- What is the advantage of commas in a conditional statement?
Redis summary
2022-06-30 Unity核心8——模型导入
Greenplum 6.x version change record common manual
如何在图片的目标中添加目标的mask
Skills that testers must know: Selenium's three waiting ways are interpreted clearly
Unity Shader入门精要初级篇(一)-- 基础光照笔记