Skip to content

Commit 8dc1fb4

Browse files
committed
+ problem 1129
1 parent 7110683 commit 8dc1fb4

File tree

5 files changed

+255
-0
lines changed

5 files changed

+255
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# 1129. Shortest Path with Alternating Colors
2+
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.
3+
4+
You are given two arrays `redEdges` and `blueEdges` where:
5+
6+
* <code>redEdges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is a directed red edge from node <code>a<sub>i</sub></code> to node <code>b<sub>i</sub></code> in the graph, and
7+
* <code>blueEdges[j] = [u<sub>j</sub>, v<sub>j</sub>]</code> indicates that there is a directed blue edge from node <code>u<sub>j</sub></code> to node <code>v<sub>j</sub></code> in the graph.
8+
9+
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.
10+
11+
#### Example 1:
12+
<pre>
13+
<strong>Input:</strong> n = 3, redEdges = [[0,1],[1,2]], blueEdges = []
14+
<strong>Output:</strong> [0,1,-1]
15+
</pre>
16+
17+
#### Example 2:
18+
<pre>
19+
<strong>Input:</strong> n = 3, redEdges = [[0,1]], blueEdges = [[2,1]]
20+
<strong>Output:</strong> [0,1,-1]
21+
</pre>
22+
23+
#### Constraints:
24+
* `1 <= n <= 100`
25+
* `0 <= redEdges.length, blueEdges.length <= 400`
26+
* `redEdges[i].length == blueEdges[j].length == 2`
27+
* <code>0 <= a<sub>i</sub>, b<sub>i</sub>, u<sub>j</sub>, v<sub>j</sub> < n</code>
28+
29+
## Solutions (Rust)
30+
31+
### 1. Solution
32+
```Rust
33+
use std::cmp::Reverse;
34+
use std::collections::BinaryHeap;
35+
use std::collections::HashSet;
36+
37+
impl Solution {
38+
pub fn shortest_alternating_paths(
39+
n: i32,
40+
red_edges: Vec<Vec<i32>>,
41+
blue_edges: Vec<Vec<i32>>,
42+
) -> Vec<i32> {
43+
let n = n as usize;
44+
let mut red_nexts = vec![vec![]; n];
45+
let mut blue_nexts = vec![vec![]; n];
46+
let mut nodes_heap = BinaryHeap::from([(Reverse(0), 0, true), (Reverse(0), 0, false)]);
47+
let mut visited = HashSet::new();
48+
let mut answer = vec![(i32::MAX, i32::MAX); n];
49+
answer[0] = (0, 0);
50+
51+
for edge in &red_edges {
52+
red_nexts[edge[0] as usize].push(edge[1] as usize);
53+
}
54+
for edge in &blue_edges {
55+
blue_nexts[edge[0] as usize].push(edge[1] as usize);
56+
}
57+
58+
while let Some((Reverse(length), node, is_red)) = nodes_heap.pop() {
59+
if visited.contains(&(node, is_red)) {
60+
continue;
61+
}
62+
63+
visited.insert((node, is_red));
64+
65+
if is_red {
66+
for &next in &blue_nexts[node] {
67+
if answer[next].1 > length + 1 {
68+
answer[next].1 = length + 1;
69+
nodes_heap.push((Reverse(length + 1), next, false));
70+
}
71+
}
72+
} else {
73+
for &next in &red_nexts[node] {
74+
if answer[next].0 > length + 1 {
75+
answer[next].0 = length + 1;
76+
nodes_heap.push((Reverse(length + 1), next, true));
77+
}
78+
}
79+
}
80+
}
81+
82+
answer
83+
.into_iter()
84+
.map(|(red, blue)| {
85+
if red.min(blue) == i32::MAX {
86+
-1
87+
} else {
88+
red.min(blue)
89+
}
90+
})
91+
.collect()
92+
}
93+
}
94+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# 1129. 颜色交替的最短路径
2+
给定一个整数 `n`,即有向图中的节点数,其中节点标记为 `0``n - 1`。图中的每条边为红色或者蓝色,并且可能存在自环或平行边。
3+
4+
给定两个数组 `redEdges``blueEdges`,其中:
5+
6+
* <code>redEdges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> 表示图中存在一条从节点 <code>a<sub>i</sub></code> 到节点 <code>b<sub>i</sub></code> 的红色有向边,
7+
* <code>blueEdges[j] = [u<sub>j</sub>, v<sub>j</sub>]</code> 表示图中存在一条从节点 <code>u<sub>j</sub></code> 到节点 <code>v<sub>j</sub></code> 的蓝色有向边。
8+
9+
返回长度为 `n` 的数组 `answer`,其中 `answer[X]` 是从节点 `0` 到节点 `X` 的红色边和蓝色边交替出现的最短路径的长度。如果不存在这样的路径,那么 `answer[x] = -1`
10+
11+
#### 示例 1:
12+
<pre>
13+
<strong>输入:</strong> n = 3, redEdges = [[0,1],[1,2]], blueEdges = []
14+
<strong>输出:</strong> [0,1,-1]
15+
</pre>
16+
17+
#### 示例 2:
18+
<pre>
19+
<strong>输入:</strong> n = 3, redEdges = [[0,1]], blueEdges = [[2,1]]
20+
<strong>输出:</strong> [0,1,-1]
21+
</pre>
22+
23+
#### 提示:
24+
* `1 <= n <= 100`
25+
* `0 <= redEdges.length, blueEdges.length <= 400`
26+
* `redEdges[i].length == blueEdges[j].length == 2`
27+
* <code>0 <= a<sub>i</sub>, b<sub>i</sub>, u<sub>j</sub>, v<sub>j</sub> < n</code>
28+
29+
## 题解 (Rust)
30+
31+
### 1. 题解
32+
```Rust
33+
use std::cmp::Reverse;
34+
use std::collections::BinaryHeap;
35+
use std::collections::HashSet;
36+
37+
impl Solution {
38+
pub fn shortest_alternating_paths(
39+
n: i32,
40+
red_edges: Vec<Vec<i32>>,
41+
blue_edges: Vec<Vec<i32>>,
42+
) -> Vec<i32> {
43+
let n = n as usize;
44+
let mut red_nexts = vec![vec![]; n];
45+
let mut blue_nexts = vec![vec![]; n];
46+
let mut nodes_heap = BinaryHeap::from([(Reverse(0), 0, true), (Reverse(0), 0, false)]);
47+
let mut visited = HashSet::new();
48+
let mut answer = vec![(i32::MAX, i32::MAX); n];
49+
answer[0] = (0, 0);
50+
51+
for edge in &red_edges {
52+
red_nexts[edge[0] as usize].push(edge[1] as usize);
53+
}
54+
for edge in &blue_edges {
55+
blue_nexts[edge[0] as usize].push(edge[1] as usize);
56+
}
57+
58+
while let Some((Reverse(length), node, is_red)) = nodes_heap.pop() {
59+
if visited.contains(&(node, is_red)) {
60+
continue;
61+
}
62+
63+
visited.insert((node, is_red));
64+
65+
if is_red {
66+
for &next in &blue_nexts[node] {
67+
if answer[next].1 > length + 1 {
68+
answer[next].1 = length + 1;
69+
nodes_heap.push((Reverse(length + 1), next, false));
70+
}
71+
}
72+
} else {
73+
for &next in &red_nexts[node] {
74+
if answer[next].0 > length + 1 {
75+
answer[next].0 = length + 1;
76+
nodes_heap.push((Reverse(length + 1), next, true));
77+
}
78+
}
79+
}
80+
}
81+
82+
answer
83+
.into_iter()
84+
.map(|(red, blue)| {
85+
if red.min(blue) == i32::MAX {
86+
-1
87+
} else {
88+
red.min(blue)
89+
}
90+
})
91+
.collect()
92+
}
93+
}
94+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use std::cmp::Reverse;
2+
use std::collections::BinaryHeap;
3+
use std::collections::HashSet;
4+
5+
impl Solution {
6+
pub fn shortest_alternating_paths(
7+
n: i32,
8+
red_edges: Vec<Vec<i32>>,
9+
blue_edges: Vec<Vec<i32>>,
10+
) -> Vec<i32> {
11+
let n = n as usize;
12+
let mut red_nexts = vec![vec![]; n];
13+
let mut blue_nexts = vec![vec![]; n];
14+
let mut nodes_heap = BinaryHeap::from([(Reverse(0), 0, true), (Reverse(0), 0, false)]);
15+
let mut visited = HashSet::new();
16+
let mut answer = vec![(i32::MAX, i32::MAX); n];
17+
answer[0] = (0, 0);
18+
19+
for edge in &red_edges {
20+
red_nexts[edge[0] as usize].push(edge[1] as usize);
21+
}
22+
for edge in &blue_edges {
23+
blue_nexts[edge[0] as usize].push(edge[1] as usize);
24+
}
25+
26+
while let Some((Reverse(length), node, is_red)) = nodes_heap.pop() {
27+
if visited.contains(&(node, is_red)) {
28+
continue;
29+
}
30+
31+
visited.insert((node, is_red));
32+
33+
if is_red {
34+
for &next in &blue_nexts[node] {
35+
if answer[next].1 > length + 1 {
36+
answer[next].1 = length + 1;
37+
nodes_heap.push((Reverse(length + 1), next, false));
38+
}
39+
}
40+
} else {
41+
for &next in &red_nexts[node] {
42+
if answer[next].0 > length + 1 {
43+
answer[next].0 = length + 1;
44+
nodes_heap.push((Reverse(length + 1), next, true));
45+
}
46+
}
47+
}
48+
}
49+
50+
answer
51+
.into_iter()
52+
.map(|(red, blue)| {
53+
if red.min(blue) == i32::MAX {
54+
-1
55+
} else {
56+
red.min(blue)
57+
}
58+
})
59+
.collect()
60+
}
61+
}

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,7 @@
652652
[1122][1122l]|[Relative Sort Array][1122] |![rs]
653653
[1123][1123l]|[Lowest Common Ancestor of Deepest Leaves][1123] |![py]
654654
[1128][1128l]|[Number of Equivalent Domino Pairs][1128] |![rs]
655+
[1129][1129l]|[Shortest Path with Alternating Colors][1129] |![rs]
655656
[1131][1131l]|[Maximum of Absolute Value Expression][1131] |![rs]
656657
[1137][1137l]|[N-th Tribonacci Number][1137] |![rs]
657658
[1138][1138l]|[Alphabet Board Path][1138] |![rb]&nbsp;&nbsp;![rs]
@@ -2004,6 +2005,7 @@
20042005
[1122]:Problemset/1122-Relative%20Sort%20Array/README.md#1122-relative-sort-array
20052006
[1123]:Problemset/1123-Lowest%20Common%20Ancestor%20of%20Deepest%20Leaves/README.md#1123-lowest-common-ancestor-of-deepest-leaves
20062007
[1128]:Problemset/1128-Number%20of%20Equivalent%20Domino%20Pairs/README.md#1128-number-of-equivalent-domino-pairs
2008+
[1129]:Problemset/1129-Shortest%20Path%20with%20Alternating%20Colors/README.md#1129-shortest-path-with-alternating-colors
20072009
[1131]:Problemset/1131-Maximum%20of%20Absolute%20Value%20Expression/README.md#1131-maximum-of-absolute-value-expression
20082010
[1137]:Problemset/1137-N-th%20Tribonacci%20Number/README.md#1137-n-th-tribonacci-number
20092011
[1138]:Problemset/1138-Alphabet%20Board%20Path/README.md#1138-alphabet-board-path
@@ -3359,6 +3361,7 @@
33593361
[1122l]:https://leetcode.com/problems/relative-sort-array/
33603362
[1123l]:https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/
33613363
[1128l]:https://leetcode.com/problems/number-of-equivalent-domino-pairs/
3364+
[1129l]:https://leetcode.com/problems/shortest-path-with-alternating-colors/
33623365
[1131l]:https://leetcode.com/problems/maximum-of-absolute-value-expression/
33633366
[1137l]:https://leetcode.com/problems/n-th-tribonacci-number/
33643367
[1138l]:https://leetcode.com/problems/alphabet-board-path/

README_CN.md

+3
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,7 @@
652652
[1122][1122l]|[数组的相对排序][1122] |![rs]
653653
[1123][1123l]|[最深叶节点的最近公共祖先][1123] |![py]
654654
[1128][1128l]|[等价多米诺骨牌对的数量][1128] |![rs]
655+
[1129][1129l]|[颜色交替的最短路径][1129] |![rs]
655656
[1131][1131l]|[绝对值表达式的最大值][1131] |![rs]
656657
[1137][1137l]|[第 N 个泰波那契数][1137] |![rs]
657658
[1138][1138l]|[字母板上的路径][1138] |![rb]&nbsp;&nbsp;![rs]
@@ -2004,6 +2005,7 @@
20042005
[1122]:Problemset/1122-Relative%20Sort%20Array/README_CN.md#1122-数组的相对排序
20052006
[1123]:Problemset/1123-Lowest%20Common%20Ancestor%20of%20Deepest%20Leaves/README_CN.md#1123-最深叶节点的最近公共祖先
20062007
[1128]:Problemset/1128-Number%20of%20Equivalent%20Domino%20Pairs/README_CN.md#1128-等价多米诺骨牌对的数量
2008+
[1129]:Problemset/1129-Shortest%20Path%20with%20Alternating%20Colors/README_CN.md#1129-颜色交替的最短路径
20072009
[1131]:Problemset/1131-Maximum%20of%20Absolute%20Value%20Expression/README_CN.md#1131-绝对值表达式的最大值
20082010
[1137]:Problemset/1137-N-th%20Tribonacci%20Number/README_CN.md#1137-第-n-个泰波那契数
20092011
[1138]:Problemset/1138-Alphabet%20Board%20Path/README_CN.md#1138-字母板上的路径
@@ -3359,6 +3361,7 @@
33593361
[1122l]:https://leetcode.cn/problems/relative-sort-array/
33603362
[1123l]:https://leetcode.cn/problems/lowest-common-ancestor-of-deepest-leaves/
33613363
[1128l]:https://leetcode.cn/problems/number-of-equivalent-domino-pairs/
3364+
[1129l]:https://leetcode.cn/problems/shortest-path-with-alternating-colors/
33623365
[1131l]:https://leetcode.cn/problems/maximum-of-absolute-value-expression/
33633366
[1137l]:https://leetcode.cn/problems/n-th-tribonacci-number/
33643367
[1138l]:https://leetcode.cn/problems/alphabet-board-path/

0 commit comments

Comments
 (0)