815. Bus Routes
Description
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
Solutions
Solution: Breadth-First Search + Hash Map
- Time complexity: O(m*n)
- Space complexity: O(m*n+m)
JavaScript
js
/**
* @param {number[][]} routes
* @param {number} source
* @param {number} target
* @return {number}
*/
const numBusesToDestination = function (routes, source, target) {
if (source === target) return 0;
const stopsMap = routes.reduce((map, stops, bus) => {
for (const stop of stops) {
const buses = map.get(stop) ?? [];
buses.push(bus);
map.set(stop, buses);
}
return map;
}, new Map());
const visitedBus = new Set();
const visitedStop = new Set([source]);
let queue = [source];
let result = 1;
while (queue.length) {
const nextQueue = [];
for (const element of queue) {
const buses = stopsMap.get(element) ?? [];
for (const bus of buses) {
if (visitedBus.has(bus)) continue;
visitedBus.add(bus);
for (const stop of routes[bus]) {
if (stop === target) return result;
if (visitedStop.has(stop)) continue;
visitedStop.add(stop);
nextQueue.push(stop);
}
}
}
queue = nextQueue;
result += 1;
}
return -1;
};