当前位置:网站首页>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 !
边栏推荐
- Introduction to data fragmentation
- 路由信息协议——RIP
- [Yugong series] February 2022 U3D full stack class 005 unity engine view
- Implement custom memory allocator
- [南京大学]-[软件分析]课程学习笔记(一)-introduction
- [Nanjing University] - [software analysis] course learning notes (I) -introduction
- Output a spiral matrix C language
- Isomorphic C language
- Synchronized underlying principle, volatile keyword analysis
- 如何统计项目代码行数
猜你喜欢
Panel display technology: LCD and OLED
NCS Chengdu New Electric interview Experience
2022-06-30 Unity核心8——模型导入
硬核分享:硬件工程师常用工具包
数据分析方法论与前人经验总结2【笔记干货】
数据分片介绍
面试题:高速PCB一般布局、布线原则
Greenplum 6.x build_ install
[Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
数字三角形模型 AcWing 275. 传纸条
随机推荐
【踩坑】nacos注册一直连接localhost:8848,no available server
Redis fault handling "can't save in background: fork: cannot allocate memory“
opencv 将16位图像数据转为8位、8转16
Goldbach conjecture C language
ncs成都新电面试经验
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
Greenplum 6.x build_ Environment configuration
FPGA knowledge accumulation [6]
Implement custom memory allocator
Calf problem
Original collection of hardware bear (updated on June 2022)
Pointer advanced, string function
Output all composite numbers between 6 and 1000
硬件大熊原创合集(2022/05更新)
Image segmentation in opencv
9c09730c0eea36d495c3ff6efe3708d8
Platformization, a fulcrum of strong chain complementing chain
How to realize sliding operation component in fast application
Analysis of abnormal channel number information before and after AGC re signature service
[南京大学]-[软件分析]课程学习笔记(一)-introduction