1129. Shortest Path with Alternating Colors
原题链接在这里:https://leetcode.com/problems/shortest-path-with-alternating-colors/
题目:
Consider a directed graph, with nodes labelled 0, 1, ..., n-1
. In this graph, each edge is either red or blue, and there could be self-edges or parallel edges.
Each [i, j]
in red_edges
denotes a red directed edge from node i
to node j
. Similarly, each [i, j]
in blue_edges
denotes a blue directed edge from node i
to node j
.
Return an array answer
of length n
, where each answer[X]
is the length of the shortest path from node 0
to node X
such that the edge colors alternate along the path (or -1
if such a path doesn't exist).
Example 1:
Input: n = 3, red_edges = [[0,1],[1,2]], blue_edges = []
Output: [0,1,-1]
Example 2:
Input: n = 3, red_edges = [[0,1]], blue_edges = [[2,1]]
Output: [0,1,-1]
Example 3:
Input: n = 3, red_edges = [[1,0]], blue_edges = [[2,1]]
Output: [0,-1,-1]
Example 4:
Input: n = 3, red_edges = [[0,1]], blue_edges = [[1,2]]
Output: [0,1,2]
Example 5:
Input: n = 3, red_edges = [[0,1],[0,2]], blue_edges = [[1,0]]
Output: [0,1,1]
Constraints:
1 <= n <= 100
red_edges.length <= 400
blue_edges.length <= 400
red_edges[i].length == blue_edges[i].length == 2
0 <= red_edges[i][j], blue_edges[i][j] < n
题解:
To calculate the shortest path, use BFS.
Construct the graph, for each node, put its neighbors into 2 different sets based on edge colors.
res array also have 2 rows, shortest paths from different edges.
Add {0, 0}, {1, 0} into queue representing starting node with different colored edges.
When pulling out cur. cur[0] is the color going out, cur[1] is the node i. find it neighbor by graph[cur[0]][cur[1]].
For each neighbor, it needs to spread out with different color 1- cur[0]. Thus check res[1-cur[0]][nei].
If it has been visited before, it should still be Integer.MAX_VALUE, update it with current step res[cur[0]][cur[1]] + 1. And add it to queue.
Finally get the smallest steps for each of the nodes.
Note: Set<Integer> [][] graph = new Set[2][n]. Declared type must include type Integer. Constructor can't put <Integer> after Set.
Time Complexity: O(n+E). Perform 2 BFS iterations. E = red_edges.length + blue_edges.length.
Space: O(n).
AC Java:
class Solution {
public int[] shortestAlternatingPaths(int n, int[][] red_edges, int[][] blue_edges) {
Set<Integer> [][] graph = new Set[2][n];
for(int i = 0; i<n; i++){
graph[0][i] = new HashSet<>();
graph[1][i] = new HashSet<>();
} for(int [] red : red_edges){
graph[0][red[0]].add(red[1]);
} for(int [] blue : blue_edges){
graph[1][blue[0]].add(blue[1]);
} int [][] res = new int[2][n];
for(int i = 1; i<n; i++){
res[0][i] = Integer.MAX_VALUE;
res[1][i] = Integer.MAX_VALUE;
} LinkedList<int []> que = new LinkedList<int []>();
que.add(new int[] {0, 0});
que.add(new int[] {1, 0});
while(!que.isEmpty()){
int [] cur = que.poll();
int row = cur[0];
int i = cur[1];
for(int nei : graph[row][i]){
if(res[1-row][nei] == Integer.MAX_VALUE){
res[1-row][nei] = res[row][i]+1;
que.add(new int[]{1-row, nei});
}
}
} int [] resArr = new int[n];
for(int i = 0; i<n; i++){
int min = Math.min(res[0][i], res[1][i]);
resArr[i] = min == Integer.MAX_VALUE ? -1 : min;
} return resArr;
}
}
1129. Shortest Path with Alternating Colors的更多相关文章
- 【leetcode】1129. Shortest Path with Alternating Colors
题目如下: Consider a directed graph, with nodes labelled 0, 1, ..., n-1. In this graph, each edge is ei ...
- hdu-----(2807)The Shortest Path(矩阵+Floyd)
The Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- zoj 2760 How Many Shortest Path 最大流
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 Given a weighted directed graph ...
- The Shortest Path in Nya Graph
Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...
- hdu 3631 Shortest Path(Floyd)
题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...
- Shortest Path(思维,dfs)
Shortest Path Accepts: 40 Submissions: 610 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: ...
- Shortest Path
Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- (中等) HDU 4725 The Shortest Path in Nya Graph,Dijkstra+加点。
Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...
- 【ZOJ2760】How Many Shortest Path
How Many Shortest Path 标签: 网络流 描述 Given a weighted directed graph, we define the shortest path as th ...
随机推荐
- Akka-CQRS(9)- gRPC,实现前端设备与平台系统的高效集成
前面我们完成了一个CQRS模式的数据采集(录入)平台.可以预见:数据的产生是在线下各式各样的终端系统中,包括web.桌面.移动终端.那么,为了实现一个完整的系统,必须把前端设备通过某种网络连接形式与数 ...
- Linux node.js安装
1.下载地址 下载node 英文网址:https://nodejs.org/en/download/ 中文网址:http://nodejs.cn/download/ 2.下载下来的tar文件上传到服务 ...
- SAS学习笔记1
数据采样 简单随机抽样,从sashelp数据集中air数据文件中选取30个数 数据探索 数字特征的探索:均值.频数.最大值.最小值.众数.中位数.方差.标准差 数字分布的探索:是否服从正态分布 连续型 ...
- 关于java中是引用传递还是值传递的问题
关于JAVA中参数传递问题有两种,一种是按值传递(如果是基本类型),另一种是按引用传递(如果是對象).首先以两个例子开始:1)public class Test2 { public static vo ...
- Oracle队列实现
Oracle队列实现 -- 核心技术点:for update 创建测试表 create table t ( id number primary key, processed_flag va ...
- Java 读取控制台输入
方式1:InputStreamReader+BufferedReader package my_package; import java.io.BufferedReader; import java. ...
- ResourceDictionary文件排序方法
默认生成的ResourceDictionary文件是根据主键的hashcode排序生成的,如果想按主键排序生成是不可能的. 可以使用Xml的处理方法来生成ResourceDictionary文件. 1 ...
- 【TTS】传输表空间Linux asm -> AIX asm
[TTS]传输表空间Linux asm -> AIX asm 一.1 BLOG文档结构图 一.2 前言部分 一.2.1 导读和注意事项 各位技术爱好者,看完本文后,你可以掌 ...
- Node: 开发命令行程序英文版 (Create Your Own CLI)
CLI, as an abbreviation of Command-line Interface, can receive user's input and give an immediate re ...
- JMeter java.net.BindException: Address already in use: connect
原文:https://blog.csdn.net/macwhirr123/article/details/77199057 出现原因:TCP/IP连接数不够或TIME_WAIT中存在很多链接,导致吞吐 ...