|
| 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 | +``` |
0 commit comments