Skip to content

Commit 288860a

Browse files
committed
+ problem 815
1 parent 602403a commit 288860a

File tree

5 files changed

+229
-0
lines changed

5 files changed

+229
-0
lines changed

Problemset/0815-Bus Routes/README.md

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# 815. Bus Routes
2+
You are given an array `routes` representing bus routes where `routes[i]` is a bus route that the <code>i<sup>th</sup></code> bus repeats forever.
3+
4+
* For example, if `routes[0] = [1, 5, 7]`, this means that the <code>0<sup>th</sup></code> bus travels in the sequence `1 -> 5 -> 7 -> 1 -> 5 -> 7 -> 1 -> ...` forever.
5+
6+
You will start at the bus stop `source` (You are not on any bus initially), and you want to go to the bus stop `target`. You can travel between bus stops by buses only.
7+
8+
Return *the least number of buses you must take to travel from* `source` *to* `target`. Return `-1` if it is not possible.
9+
10+
#### Example 1:
11+
<pre>
12+
<strong>Input:</strong> routes = [[1,2,7],[3,6,7]], source = 1, target = 6
13+
<strong>Output:</strong> 2
14+
<strong>Explanation:</strong> The best strategy is take the first bus to the bus stop 7, then take the second bus to the bus stop 6.
15+
</pre>
16+
17+
#### Example 2:
18+
<pre>
19+
<strong>Input:</strong> routes = [[7,12],[4,5,15],[6],[15,19],[9,12,13]], source = 15, target = 12
20+
<strong>Output:</strong> -1
21+
</pre>
22+
23+
#### Constraints:
24+
* `1 <= routes.length <= 500`.
25+
* <code>1 <= routes[i].length <= 10<sup>5</sup></code>
26+
* All the values of `routes[i]` are **unique**.
27+
* <code>sum(routes[i].length) <= 10<sup>5</sup></code>
28+
* <code>0 <= routes[i][j] < 10<sup>6</sup></code>
29+
* <code>0 <= source, target < 10<sup>6</sup></code>
30+
31+
## Solutions (Rust)
32+
33+
### 1. Solution
34+
```Rust
35+
use std::collections::HashMap;
36+
use std::collections::HashSet;
37+
use std::collections::VecDeque;
38+
39+
impl Solution {
40+
pub fn num_buses_to_destination(routes: Vec<Vec<i32>>, source: i32, target: i32) -> i32 {
41+
if source == target {
42+
return 0;
43+
}
44+
45+
let mut routes = routes
46+
.into_iter()
47+
.map(|route| route.into_iter().collect::<HashSet<_>>())
48+
.collect::<Vec<_>>();
49+
let mut deque = VecDeque::new();
50+
let mut visited = HashSet::new();
51+
let mut neighbors = vec![vec![]; routes.len()];
52+
53+
for i in 0..routes.len() {
54+
if routes[i].contains(&source) {
55+
deque.push_back((i, 1));
56+
visited.insert(i);
57+
}
58+
59+
for j in i + 1..routes.len() {
60+
for stop in routes[i].iter() {
61+
if routes[j].contains(stop) {
62+
neighbors[i].push(j);
63+
neighbors[j].push(i);
64+
break;
65+
}
66+
}
67+
}
68+
}
69+
70+
while let Some((i, buses)) = deque.pop_front() {
71+
if routes[i].contains(&target) {
72+
return buses;
73+
}
74+
75+
for &j in &neighbors[i] {
76+
if !visited.contains(&j) {
77+
deque.push_back((j, buses + 1));
78+
visited.insert(j);
79+
}
80+
}
81+
}
82+
83+
-1
84+
}
85+
}
86+
```
+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# 815. 公交路线
2+
给你一个数组 `routes` ,表示一系列公交线路,其中每个 `routes[i]` 表示一条公交线路,第 `i` 辆公交车将会在上面循环行驶。
3+
4+
* 例如,路线 `routes[0] = [1, 5, 7]` 表示第 `0` 辆公交车会一直按序列 `1 -> 5 -> 7 -> 1 -> 5 -> 7 -> 1 -> ...` 这样的车站路线行驶。
5+
6+
现在从 `source` 车站出发(初始时不在公交车上),要前往 `target` 车站。 期间仅可乘坐公交车。
7+
8+
求出 **最少乘坐的公交车数量** 。如果不可能到达终点车站,返回 `-1`
9+
10+
#### 示例 1:
11+
<pre>
12+
<strong>输入:</strong> routes = [[1,2,7],[3,6,7]], source = 1, target = 6
13+
<strong>输出:</strong> 2
14+
<strong>解释:</strong> 最优策略是先乘坐第一辆公交车到达车站 7 , 然后换乘第二辆公交车到车站 6 。
15+
</pre>
16+
17+
#### 示例 2:
18+
<pre>
19+
<strong>输入:</strong> routes = [[7,12],[4,5,15],[6],[15,19],[9,12,13]], source = 15, target = 12
20+
<strong>输出:</strong> -1
21+
</pre>
22+
23+
#### 提示:
24+
* `1 <= routes.length <= 500`.
25+
* <code>1 <= routes[i].length <= 10<sup>5</sup></code>
26+
* `routes[i]` 中的所有值 **互不相同**
27+
* <code>sum(routes[i].length) <= 10<sup>5</sup></code>
28+
* <code>0 <= routes[i][j] < 10<sup>6</sup></code>
29+
* <code>0 <= source, target < 10<sup>6</sup></code>
30+
31+
## 题解 (Rust)
32+
33+
### 1. 题解
34+
```Rust
35+
use std::collections::HashMap;
36+
use std::collections::HashSet;
37+
use std::collections::VecDeque;
38+
39+
impl Solution {
40+
pub fn num_buses_to_destination(routes: Vec<Vec<i32>>, source: i32, target: i32) -> i32 {
41+
if source == target {
42+
return 0;
43+
}
44+
45+
let mut routes = routes
46+
.into_iter()
47+
.map(|route| route.into_iter().collect::<HashSet<_>>())
48+
.collect::<Vec<_>>();
49+
let mut deque = VecDeque::new();
50+
let mut visited = HashSet::new();
51+
let mut neighbors = vec![vec![]; routes.len()];
52+
53+
for i in 0..routes.len() {
54+
if routes[i].contains(&source) {
55+
deque.push_back((i, 1));
56+
visited.insert(i);
57+
}
58+
59+
for j in i + 1..routes.len() {
60+
for stop in routes[i].iter() {
61+
if routes[j].contains(stop) {
62+
neighbors[i].push(j);
63+
neighbors[j].push(i);
64+
break;
65+
}
66+
}
67+
}
68+
}
69+
70+
while let Some((i, buses)) = deque.pop_front() {
71+
if routes[i].contains(&target) {
72+
return buses;
73+
}
74+
75+
for &j in &neighbors[i] {
76+
if !visited.contains(&j) {
77+
deque.push_back((j, buses + 1));
78+
visited.insert(j);
79+
}
80+
}
81+
}
82+
83+
-1
84+
}
85+
}
86+
```
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use std::collections::HashMap;
2+
use std::collections::HashSet;
3+
use std::collections::VecDeque;
4+
5+
impl Solution {
6+
pub fn num_buses_to_destination(routes: Vec<Vec<i32>>, source: i32, target: i32) -> i32 {
7+
if source == target {
8+
return 0;
9+
}
10+
11+
let mut routes = routes
12+
.into_iter()
13+
.map(|route| route.into_iter().collect::<HashSet<_>>())
14+
.collect::<Vec<_>>();
15+
let mut deque = VecDeque::new();
16+
let mut visited = HashSet::new();
17+
let mut neighbors = vec![vec![]; routes.len()];
18+
19+
for i in 0..routes.len() {
20+
if routes[i].contains(&source) {
21+
deque.push_back((i, 1));
22+
visited.insert(i);
23+
}
24+
25+
for j in i + 1..routes.len() {
26+
for stop in routes[i].iter() {
27+
if routes[j].contains(stop) {
28+
neighbors[i].push(j);
29+
neighbors[j].push(i);
30+
break;
31+
}
32+
}
33+
}
34+
}
35+
36+
while let Some((i, buses)) = deque.pop_front() {
37+
if routes[i].contains(&target) {
38+
return buses;
39+
}
40+
41+
for &j in &neighbors[i] {
42+
if !visited.contains(&j) {
43+
deque.push_back((j, buses + 1));
44+
visited.insert(j);
45+
}
46+
}
47+
}
48+
49+
-1
50+
}
51+
}

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@
544544
[812][812l] |[Largest Triangle Area][812] |![rs]
545545
[813][813l] |[Largest Sum of Averages][813] |![py]
546546
[814][814l] |[Binary Tree Pruning][814] |![py]&nbsp;&nbsp;![rb]
547+
[815][815l] |[Bus Routes][815] |![rs]
547548
[816][816l] |[Ambiguous Coordinates][816] |![rs]
548549
[817][817l] |[Linked List Components][817] |![py]&nbsp;&nbsp;![rb]
549550
[819][819l] |[Most Common Word][819] |![py]
@@ -2164,6 +2165,7 @@
21642165
[812]:Problemset/0812-Largest%20Triangle%20Area/README.md#812-largest-triangle-area
21652166
[813]:Problemset/0813-Largest%20Sum%20of%20Averages/README.md#813-largest-sum-of-averages
21662167
[814]:Problemset/0814-Binary%20Tree%20Pruning/README.md#814-binary-tree-pruning
2168+
[815]:Problemset/0815-Bus%20Routes/README.md#815-bus-routes
21672169
[816]:Problemset/0816-Ambiguous%20Coordinates/README.md#816-ambiguous-coordinates
21682170
[817]:Problemset/0817-Linked%20List%20Components/README.md#817-linked-list-components
21692171
[819]:Problemset/0819-Most%20Common%20Word/README.md#819-most-common-word
@@ -3778,6 +3780,7 @@
37783780
[812l]:https://leetcode.com/problems/largest-triangle-area/
37793781
[813l]:https://leetcode.com/problems/largest-sum-of-averages/
37803782
[814l]:https://leetcode.com/problems/binary-tree-pruning/
3783+
[815l]:https://leetcode.com/problems/bus-routes/
37813784
[816l]:https://leetcode.com/problems/ambiguous-coordinates/
37823785
[817l]:https://leetcode.com/problems/linked-list-components/
37833786
[819l]:https://leetcode.com/problems/most-common-word/

README_CN.md

+3
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@
544544
[812][812l] |[最大三角形面积][812] |![rs]
545545
[813][813l] |[最大平均值和的分组][813] |![py]
546546
[814][814l] |[二叉树剪枝][814] |![py]&nbsp;&nbsp;![rb]
547+
[815][815l] |[公交路线][815] |![rs]
547548
[816][816l] |[模糊坐标][816] |![rs]
548549
[817][817l] |[链表组件][817] |![py]&nbsp;&nbsp;![rb]
549550
[819][819l] |[最常见的单词][819] |![py]
@@ -2164,6 +2165,7 @@
21642165
[812]:Problemset/0812-Largest%20Triangle%20Area/README_CN.md#812-最大三角形面积
21652166
[813]:Problemset/0813-Largest%20Sum%20of%20Averages/README_CN.md#813-最大平均值和的分组
21662167
[814]:Problemset/0814-Binary%20Tree%20Pruning/README_CN.md#814-二叉树剪枝
2168+
[815]:Problemset/0815-Bus%20Routes/README_CN.md#815-公交路线
21672169
[816]:Problemset/0816-Ambiguous%20Coordinates/README_CN.md#816-模糊坐标
21682170
[817]:Problemset/0817-Linked%20List%20Components/README_CN.md#817-链表组件
21692171
[819]:Problemset/0819-Most%20Common%20Word/README_CN.md#819-最常见的单词
@@ -3778,6 +3780,7 @@
37783780
[812l]:https://leetcode.cn/problems/largest-triangle-area/
37793781
[813l]:https://leetcode.cn/problems/largest-sum-of-averages/
37803782
[814l]:https://leetcode.cn/problems/binary-tree-pruning/
3783+
[815l]:https://leetcode.cn/problems/bus-routes/
37813784
[816l]:https://leetcode.cn/problems/ambiguous-coordinates/
37823785
[817l]:https://leetcode.cn/problems/linked-list-components/
37833786
[819l]:https://leetcode.cn/problems/most-common-word/

0 commit comments

Comments
 (0)