LeetCode 1059. All Paths from Source Lead to Destination
原题链接在这里:https://leetcode.com/problems/all-paths-from-source-lead-to-destination/
题目:
Given the edges of a directed graph, and two nodes source and destination of this graph, determine whether or not all paths starting from source eventually end at destination, that is:
- At least one path exists from the
sourcenode to thedestinationnode - If a path exists from the
sourcenode to a node with no outgoing edges, then that node is equal todestination. - The number of possible paths from
sourcetodestinationis a finite number.
Return true if and only if all roads from source lead to destination.
Example 1:

Input: n = 3, edges = [[0,1],[0,2]], source = 0, destination = 2
Output: false
Explanation: It is possible to reach and get stuck on both node 1 and node 2.
Example 2:

Input: n = 4, edges = [[0,1],[0,3],[1,2],[2,1]], source = 0, destination = 3
Output: false
Explanation: We have two possibilities: to end at node 3, or to loop over node 1 and node 2 indefinitely.
Example 3:

Input: n = 4, edges = [[0,1],[0,2],[1,3],[2,3]], source = 0, destination = 3
Output: true
Example 4:

Input: n = 3, edges = [[0,1],[1,1],[1,2]], source = 0, destination = 2
Output: false
Explanation: All paths from the source node end at the destination node, but there are an infinite number of paths, such as 0-1-2, 0-1-1-2, 0-1-1-1-2, 0-1-1-1-1-2, and so on.
Example 5:

Input: n = 2, edges = [[0,1],[1,1]], source = 0, destination = 1
Output: false
Explanation: There is infinite self-loop at destination node.
Note:
- The given graph may have self loops and parallel edges.
- The number of nodes
nin the graph is between1and10000 - The number of edges in the graph is between
0and10000 0 <= edges.length <= 10000edges[i].length == 20 <= source <= n - 10 <= destination <= n - 1
题解:
There are 2 cases it should return false.
case 1: it encounters a node that has no outgoing edges, but it is not destination.
case 2: it has cycle.
Otherwise, it returns true.
Could iterate graph with BFS. When indegree of a node becomes negative, then ther is cycle.
Time Complexity: O(n+e). e = edges.length.
Space: O(n+e).
AC Java:
class Solution {
public boolean leadsToDestination(int n, int[][] edges, int source, int destination) {
Set<Integer> [] graph = new Set[n];
for(int i = 0; i<n; i++){
graph[i] = new HashSet<Integer>();
}
int [] inDegrees = new int[n];
for(int [] edge : edges){
graph[edge[0]].add(edge[1]);
inDegrees[edge[1]]++;
}
LinkedList<Integer> que = new LinkedList<Integer>();
que.add(source);
while(!que.isEmpty()){
int cur = que.poll();
if(graph[cur].size() == 0 && cur != destination){
return false;
}
for(int nei : graph[cur]){
if(inDegrees[nei] < 0){
return false;
}
inDegrees[nei]--;
que.add(nei);
}
}
return true;
}
}
Could iterate by DFS too.
If current node has been visited within current DFS, then there is cycle.
When traversing all the nodes, make current node as done.
Time Complexity: O(n+e).
Space: O(n+e).
AC Java:
class Solution {
public boolean leadsToDestination(int n, int[][] edges, int source, int destination) {
Set<Integer> [] graph = new Set[n];
for(int i = 0; i<n; i++){
graph[i] = new HashSet<Integer>();
}
for(int [] edge : edges){
graph[edge[0]].add(edge[1]);
}
return dfs(source, destination, graph, new int[n]);
}
private boolean dfs(int cur, int destination, Set<Integer> [] graph, int [] visited){
if(visited[cur] != 0){
return visited[cur] == 2;
}
if(graph[cur].size() == 0){
return cur == destination;
}
visited[cur] = 1;
for(int nei : graph[cur]){
if(!dfs(nei, destination, graph, visited)){
return false;
}
}
visited[cur] = 2;
return true;
}
}
LeetCode 1059. All Paths from Source Lead to Destination的更多相关文章
- LeetCode 797. All Paths From Source to Target
题目链接:https://leetcode.com/problems/all-paths-from-source-to-target/description/ Given a directed, ac ...
- 【leetcode】All Paths From Source to Target
题目如下: Given a directed, acyclic graph of N nodes. Find all possible paths from node 0 to node N-1, a ...
- LeetCode 63. Unique Paths II不同路径 II (C++/Java)
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- [LeetCode] 62. Unique Paths 唯一路径
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- Data Flow ->> Raw File Source & Raw File Destination
Raw File Source & Raw File Destination一般用在当有某个package在导入数据或者处理数据需要花费非常长的时间的情况下,可以通过把一些处理好的数据先存到r ...
- [LeetCode] All Paths From Source to Target 从起点到目标点到所有路径
Given a directed, acyclic graph of N nodes. Find all possible paths from node 0 to node N-1, and re ...
- 【LeetCode】797. All Paths From Source to Target 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 75th LeetCode Weekly Contest All Paths From Source to Target
Given a directed, acyclic graph of N nodes. Find all possible paths from node 0 to node N-1, and re ...
- 【leetcode】797. All Paths From Source to Target
Given a directed acyclic graph (DAG) of n nodes labeled from 0 to n - 1, find all possible paths fro ...
随机推荐
- STL源码剖析——算法#1 内存处理基本工具
我们在学习序列式容器时,我们经常会遇到这三个函数:uninitialized_copy.uninitialized_fill.uninitialized_fill_n.在那时我们只是仅仅知道这些函数的 ...
- Python 入门(2):数据类型
一 Number(数字) 1.1 数字类型的创建 a = 10 b = a b = 5 print(a) 10 print(b) 5 1.2 Number 类型转换 a = 5.2 b = 5 c = ...
- 警告:MySQL-server-5.6.26-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
警告:MySQL-server--.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY 错误:依赖检测失败: /usr/bin/ ...
- nrm的安装和使用
1.安装nodejs,下载地址,http://nodejs.cn/download/,安装过程直接点击下一步即可 安装完成后cmd输入npm -v 查看当前安装的npm的版本,如下图提示所示则表示安装 ...
- loj#10013 曲线(三分)
题目 #10013. 「一本通 1.2 例 3」曲线 解析 首先这个题保证了所有的二次函数都是下凸的, \(F(x)=max\{s_i(x)\}i=1...n\)在每一个x上对应的最大的y,我们最后得 ...
- k8s--yml文件2
- html解决空格显示问题
在前端里面,大家都知道,html中输入空格或换行是识别不了是空格的,但是有时候需要实现,那么该如何解决呢?主要有以下几个方面: 1:常用的转义: 2:使用全角拼音,然后输入空格也可实现 3:用标签 ...
- js基本包装类型及Math对象(八)
一.基本包装类型[继承于Object类型]1.Number().String().Boolean()引用数据类型[包装类型]分别对应的基本数据类型为number.string.boolean. 2.当 ...
- 设计的一些kubernetes面试题
公司现在上了一部分的业务至k8s,老实说,我心里很慌,在项目改造中,每天都会遇到很多问题,好友找我出一份k8s面试题,参考了网上的一些,再加上自己公司遇到的一些问题,整理如下: 参考链接:http:/ ...
- Audio Queue Services Programming Guide(音频队列服务编程指南)
Audio Queue Services 的苹果官方文档: https://developer.apple.com/library/ios/documentation/MusicAudio/Conce ...