815. Bus Routes Hard
1/**
2 * [815] Bus Routes
3 *
4 * You are given an array routes representing bus routes where routes[i] is a bus route that the i^th bus repeats forever.
5 *
6 * For example, if routes[0] = [1, 5, 7], this means that the 0^th bus travels in the sequence 1 -> 5 -> 7 -> 1 -> 5 -> 7 -> 1 -> ... forever.
7 *
8 * 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.
9 * Return the least number of buses you must take to travel from source to target. Return -1 if it is not possible.
10 *
11 * Example 1:
12 *
13 * Input: routes = [[1,2,7],[3,6,7]], source = 1, target = 6
14 * Output: 2
15 * Explanation: The best strategy is take the first bus to the bus stop 7, then take the second bus to the bus stop 6.
16 *
17 * Example 2:
18 *
19 * Input: routes = [[7,12],[4,5,15],[6],[15,19],[9,12,13]], source = 15, target = 12
20 * Output: -1
21 *
22 *
23 * Constraints:
24 *
25 * 1 <= routes.length <= 500.
26 * 1 <= routes[i].length <= 10^5
27 * All the values of routes[i] are unique.
28 * sum(routes[i].length) <= 10^5
29 * 0 <= routes[i][j] < 10^6
30 * 0 <= source, target < 10^6
31 *
32 */
33pub struct Solution {}
34
35// problem: https://leetcode.com/problems/bus-routes/
36// discuss: https://leetcode.com/problems/bus-routes/discuss/?currentPage=1&orderBy=most_votes&query=
37
38// submission codes start here
39
40impl Solution {
41 pub fn num_buses_to_destination(routes: Vec<Vec<i32>>, source: i32, target: i32) -> i32 {
42 0
43 }
44}
45
46// submission codes end
47
48#[cfg(test)]
49mod tests {
50 use super::*;
51
52 #[test]
53 fn test_815() {
54 }
55}
56
Back
© 2025 bowen.ge All Rights Reserved.