Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Shortest Path with Alternating Colors

XPChallenge Points: 20
levelLevel: Medium

You are given an integer n, the number of nodes in a directed graph where the nodes are labeled from 0 to n - 1.
Each edge is red or blue in this graph, and there could be self-edges and parallel edges.
You are also given two arrays redEdges and blueEdges where:
• redEdges[i] = [ai, bi] indicates that there is a directed red edge from node ai to node bi in the graph.
• blueEdges[j] = [uj, vj] indicates that there is a directed blue edge from node uj to node vj in the graph.
Return an array answer of length n, where 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 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]

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result