1129. Shortest Path with Alternating Colors Medium

@problem@discussion
#Breadth-First Search#Graph



1/**
2 * [1129] Shortest Path with Alternating Colors
3 *
4 * 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.
5 * You are given two arrays redEdges and blueEdges where:
6 * 
7 * 	redEdges[i] = [ai, bi] indicates that there is a directed red edge from node ai to node bi in the graph, and
8 * 	blueEdges[j] = [uj, vj] indicates that there is a directed blue edge from node uj to node vj in the graph.
9 * 
10 * 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 does not exist.
11 *  
12 * Example 1:
13 * 
14 * Input: n = 3, redEdges = [[0,1],[1,2]], blueEdges = []
15 * Output: [0,1,-1]
16 * 
17 * Example 2:
18 * 
19 * Input: n = 3, redEdges = [[0,1]], blueEdges = [[2,1]]
20 * Output: [0,1,-1]
21 * 
22 *  
23 * Constraints:
24 * 
25 * 	1 <= n <= 100
26 * 	0 <= redEdges.length, blueEdges.length <= 400
27 * 	redEdges[i].length == blueEdges[j].length == 2
28 * 	0 <= ai, bi, uj, vj < n
29 * 
30 */
31pub struct Solution {}
32
33// problem: https://leetcode.com/problems/shortest-path-with-alternating-colors/
34// discuss: https://leetcode.com/problems/shortest-path-with-alternating-colors/discuss/?currentPage=1&orderBy=most_votes&query=
35
36// submission codes start here
37
38impl Solution {
39    pub fn shortest_alternating_paths(n: i32, red_edges: Vec<Vec<i32>>, blue_edges: Vec<Vec<i32>>) -> Vec<i32> {
40        vec![]
41    }
42}
43
44// submission codes end
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn test_1129() {
52    }
53}
54


Back
© 2025 bowen.ge All Rights Reserved.