You are given an array routes
representing bus routes where routes[i]
is a bus route that the ith
bus repeats forever.
- For example, if
routes[0] = [1, 5, 7]
, this means that the0th
bus travels in the sequence1 -> 5 -> 7 -> 1 -> 5 -> 7 -> 1 -> ...
forever.
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.
Return the least number of buses you must take to travel from source
to target
. Return -1
if it is not possible.
Example 1:
Input: routes = [[1,2,7],[3,6,7]], source = 1, target = 6 Output: 2 Explanation: The best strategy is take the first bus to the bus stop 7, then take the second bus to the bus stop 6.
Example 2:
Input: routes = [[7,12],[4,5,15],[6],[15,19],[9,12,13]], source = 15, target = 12 Output: -1
Constraints:
1 <= routes.length <= 500
.1 <= routes[i].length <= 105
- All the values of
routes[i]
are unique. sum(routes[i].length) <= 105
0 <= routes[i][j] < 106
0 <= source, target < 106
Companies: Uber, TikTok, PhonePe, Amazon, Google, Microsoft
Related Topics:
Array, Hash Table, Breadth-First Search
Similar Questions:
// OJ: https://leetcode.com/problems/bus-routes/
// Author: github.com/lzl124631x
// Time: O(N) where N is the sum of lengths of elements in `A`
// Space: O(N)
class Solution {
public:
int numBusesToDestination(vector<vector<int>>& A, int source, int target) {
int N = A.size(), step = 0;
unordered_map<int, vector<int>> stops; // stop -> all buses
for (int i = 0; i < N; ++i) {
for (int n : A[i]) stops[n].push_back(i);
}
unordered_set<int> seen{source}, busTaken;
queue<int> q{{source}}; // queue containing stops
while (q.size()) {
int cnt = q.size();
while (cnt--) {
int u = q.front();
q.pop();
if (u == target) return step;
for (int bus : stops[u]) {
if (busTaken.count(bus)) continue; // Don't revisit the same bus
busTaken.insert(bus);
for (int v : A[bus]) {
if (seen.count(v)) continue; // Don't revisit the same stop
q.push(v);
seen.insert(v);
}
}
}
++step;
}
return -1;
}
};
// OJ: https://leetcode.com/problems/bus-routes
// Author: github.com/lzl124631x
// Time: O(N) where N is the sum of lengths of elements in `A`
// Space: O(N)
class Solution {
public:
int numBusesToDestination(vector<vector<int>>& A, int source, int target) {
if (source == target) return 0;
int N = A.size(), step = 1;
unordered_map<int, vector<int>> m; // mapping from stop index to indices of related buses
queue<int> q; // queue containing buses
unordered_set<int> seenBus, seenStop;
for (int i = 0; i < N; ++i) {
for (int n : A[i]) {
m[n].push_back(i);
if (n == source) {
q.push(i);
seenBus.insert(i);
}
}
}
while (q.size()) {
int cnt = q.size();
while (cnt--) {
int fromBus = q.front();
q.pop();
for (int fromStop : A[fromBus]) {
if (seenStop.count(fromStop)) continue;
if (fromStop == target) return step;
seenStop.insert(fromStop);
for (int toBus : m[fromStop]) {
if (seenBus.count(toBus)) continue;
seenBus.insert(toBus);
q.push(toBus);
}
}
}
++step;
}
return -1;
}
};