LeetCode 1557. Minimum Number of Vertices to Reach All Nodes

原题链接在这里:https://leetcode.com/problems/minimum-number-of-vertices-to-reach-all-nodes/description/

题目:

Given a directed acyclic graph, with n vertices numbered from 0 to n-1, and an array edges where edges[i] = [fromi, toi] represents a directed edge from node fromi to node toi.

Find the smallest set of vertices from which all nodes in the graph are reachable. It's guaranteed that a unique solution exists.

Notice that you can return the vertices in any order.

Example 1:

Input: n = 6, edges = [[0,1],[0,2],[2,5],[3,4],[4,2]]
Output: [0,3]
Explanation: It's not possible to reach all the nodes from a single vertex. From 0 we can reach [0,1,2,5]. From 3 we can reach [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]
Explanation: Notice that vertices 0, 3 and 2 are not reachable from any other node, so we must include them. Also any of these vertices can reach nodes 1 and 4.

Constraints:

  • 2 <= n <= 10^5
  • 1 <= edges.length <= min(10^5, n * (n - 1) / 2)
  • edges[i].length == 2
  • 0 <= fromi, toi < n
  • All pairs (fromi, toi) are distinct.

题解:

All the nodes with no indegree should be in the final result.

Because node with indegree, they can be reached by some other nodes. And nodes with no indegree can't be reached by any other nodes.

Time Complexity: O(n + e). e = edges.size().

Space: O(n).

AC Java:

 1 class Solution {
 2     public List<Integer> findSmallestSetOfVertices(int n, List<List<Integer>> edges) {
 3         List<Integer> res = new ArrayList<>();
 4         int [] inDegree = new int[n];
 5         for(List<Integer> e : edges){
 6             inDegree[e.get(1)]++;
 7         }
 8 
 9         for(int i = 0; i < n; i++){
10             if(inDegree[i] == 0){
11                 res.add(i);
12             }
13         }
14 
15         return res;
16     }
17 }

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/777489.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

腾讯的cherry-markdown如何在vue项目中自定义视频播放器?【引入自定义组件版,非普通html】

调研好久没给维护的社区增加新内容了,想到社区的播放器还是浏览器原生的,看着挺丑的,因此我打算给社区的cherry-markdown渲染的markdown文章里的视频样式给改改。首先为了让事情不复杂化,我打算引入西瓜视频播放器(https://h5player.bytedance.com ),它支持的功能挺多的,…

腾讯的cherry-markdown如何在vue项目中自定义视频播放器?

调研好久没给维护的社区增加新内容了,想到社区的播放器还是浏览器原生的,看着挺丑的,因此我打算给社区的cherry-markdown渲染的markdown文章里的视频样式给改改。首先为了让事情不复杂化,我打算引入西瓜视频播放器(https://h5player.bytedance.com ),它支持的功能挺多的,…

LeetCode 1284. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix

原题链接在这里:https://leetcode.com/problems/minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix/description/ 题目: Given a m x n binary matrix mat. In one step, you can choose one cell and flip it and all the four neighbors of it if they exi…

【安全技术系列】用户实体行为分析(UEBA)

一、背景 数字新时代正在加速全面到来,网络环境变得更加多元、人员变得更复杂、接入方式多种多样,网络边界逐渐模糊甚至消失,同时伴随着企业数据的激增。数字化转型促进组织的业务发展的同时,也带来了重大的网络安全挑战。 1.越来越多的外部攻击,包括被利益驱动或国家驱动…

2024 暑假友谊赛 3

2024 暑假友谊赛 3 A - A CodeForces - 1187E 思路 设 \(f_i\) 表示以 \(i\) 为根的子树产生的贡献,则有 \(f_i=size_i+\sum\limits_{j\in son_i} f_j\),即起初选定 \(i\) 为起点后产生 \(size_i\) 的贡献,后续是它的子树产生的贡献。 但这样以不同根节点去求贡献是 \(O(n^2…

LeetCode 2189. Number of Ways to Build House of Cards

原题链接在这里:https://leetcode.com/problems/number-of-ways-to-build-house-of-cards/description/ 题目: You are given an integer n representing the number of playing cards you have. A house of cards meets the following conditions:A house of cards consists…

全网最适合入门的面向对象编程教程:31 Python的内置数据类型-对象Object和类型Type

Python 中的对象和类型是一个非常重要的概念。在 Python 中,一切都是对象,包括数字、字符串、列表等,每个对象都有自己的类型。全网最适合入门的面向对象编程教程:31 Python 的内置数据类型-对象 Object 和类型 Type摘要: Python 中的对象和类型是一个非常重要的概念。在 Pyt…

Golang语言goroutine协程并发安全及锁机制

作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任。 目录一.多协程操作同一数据问题引出二.互斥锁Mutex1 互斥锁概述2使用互斥锁Mutex同步协程三.读写互斥锁RWMutex1 读写互斥锁概述2 读写锁RWMutex引入 一.多协程操作同一数据问题引出package mainimport (&quo…

[rCore学习笔记 021]多道程序与分时任务

写在前面 本随笔是非常菜的菜鸡写的。如有问题请及时提出。 可以联系:1160712160@qq.com GitHhub:https://github.com/WindDevil (目前啥也没有 导读 这里就是第三章的开头了,由于我的巨菜,导致天天半天理解不了关键点所在,唉,实在是太折磨人. 遵照上一章开头的时候的优良传…

基于FPGA的2FSK调制解调系统,包含testbench,高斯信道模块,误码率统计模块,可以设置不同SNR

1.算法仿真效果本系统在以前写过的FSK调制解调系统的基础上,增加了高斯信道模块,误码率统计模块,可以验证不同SNR情况下的FSK误码情况。vivado2019.2仿真结果如下(完整代码运行后无水印):SNR=16dbSNR=10dbSNR=5dbSNR=0dbRTL结构图如下:2.算法涉及理论知识概要频移键控是…