Class Solution
- java.lang.Object
-
- g1101_1200.s1129_shortest_path_with_alternating_colors.Solution
-
public class Solution extends Object
1129 - Shortest Path with Alternating Colors.Medium
You are given an integer
n
, the number of nodes in a directed graph where the nodes are labeled from0
ton - 1
. Each edge is red or blue in this graph, and there could be self-edges and parallel edges.You are given two arrays
redEdges
andblueEdges
where:redEdges[i] = [ai, bi]
indicates that there is a directed red edge from nodeai
to nodebi
in the graph, andblueEdges[j] = [uj, vj]
indicates that there is a directed blue edge from nodeuj
to nodevj
in the graph.
Return an array
answer
of lengthn
, where eachanswer[x]
is the length of the shortest path from node0
to nodex
such that the edge colors alternate along the path, or-1
if such a path does not exist.Example 1:
Input: n = 3, redEdges = [[0,1],[1,2]], blueEdges = []
Output: [0,1,-1]
Example 2:
Input: n = 3, redEdges = [[0,1]], blueEdges = [[2,1]]
Output: [0,1,-1]
Constraints:
1 <= n <= 100
0 <= redEdges.length, blueEdges.length <= 400
redEdges[i].length == blueEdges[j].length == 2
0 <= ai, bi, uj, vj < n
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int[]
shortestAlternatingPaths(int n, int[][] redEdges, int[][] blueEdges)
-