Problem Statement:
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 the 0th bus travels in the sequence 1 -> 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 bus 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
CODE:
Java
C++
Python
import java.util.*; class Innoskrit { public int numBusesToDestination(int[][] routes, int source, int target) { int n = routes.length; Map<Integer, List<Integer>> map = new HashMap<>(); for(int i = 0; i < n; i++) { for(int j = 0; j < routes[i].length; j++) { int busStopNo = routes[i][j]; List<Integer> buses = map.getOrDefault(busStopNo, new ArrayList<>()); buses.add(i); map.put(busStopNo, buses); } } Queue<Integer> queue = new LinkedList<>(); Set<Integer> busVisited = new HashSet<>(); Set<Integer> busStopVisited = new HashSet<>(); queue.add(source); busStopVisited.add(source); int level = 0; while(!queue.isEmpty()) { int size = queue.size(); for(int i = 0; i < size; i++) { int currBusStop = queue.poll(); if(currBusStop == target) { return level; } List<Integer> buses = map.get(currBusStop); for(int bus : buses) { if(!busVisited.contains(bus)) { for(int busStop : routes[bus]) { if(!busStopVisited.contains(busStop)) { busStopVisited.add(busStop); queue.add(busStop); } } } busVisited.add(bus); } } level++; } return -1; } public static void main (String[] args) { Innoskrit obj = new Innoskrit(); int routes[][] = new int[][]{ {1, 2, 3}, {1, 4, 5}, {5, 8, 7}, {3, 6, 7}}; System.out.println(obj.numBusesToDestination(routes, 1, 7)); } }
#include <bits/stdc++.h> using namespace std; class Innoskrit { public: int numBusesToDestination(vector<vector<int>>& routes, int source, int target) { int n = routes.size(); unordered_map<int, vector<int>> map; for(int i = 0; i < n; i++) { for(int j = 0; j < routes[i].size(); j++) { int busStopNo = routes[i][j]; map[busStopNo].push_back(i); } } queue<int> q; set<int> busVisited; set<int> busStopVisited; q.push(source); busStopVisited.insert(source); int level = 0; while(!q.empty()) { int size = q.size(); for(int i = 0; i < size; i++) { int currBusStop = q.front(); q.pop(); if(currBusStop == target) { return level; } vector<int> buses = map[currBusStop]; for(int bus : buses) { if(busVisited.find(bus) == busVisited.end()) { for(int busStop : routes[bus]) { if(busStopVisited.find(busStop) == busStopVisited.end()) { busStopVisited.insert(busStop); q.push(busStop); } } } busVisited.insert(bus); } } level++; } return -1; } }; int main(int argc, char * argv[]) { Innoskrit obj; vector<vector<int>> board { {1, 2, 3}, {1, 4, 5}, {5, 8, 7}, {3, 6, 7}}; cout << obj.numBusesToDestination(board, 1, 7) << endl; return 0; }
from collections import deque class Innoskrit: def numBusesToDestination(self, routes, source, target): n = len(routes) hash_map = dict() for i in range(n): for j in range(len(routes[i])): bus_stop_no = routes[i][j] if bus_stop_no not in hash_map: hash_map[bus_stop_no] = list() hash_map[bus_stop_no].append(i) queue = deque() bus_visited = set() bus_stop_visited = set() queue.append(source) bus_stop_visited.add(source) level = 0 while queue: size = len(queue) for i in range(size): curr_bus_stop = queue.popleft() if curr_bus_stop == target: return level buses = hash_map[curr_bus_stop] for bus in buses: if bus not in bus_visited: for bus_stop in routes[bus]: if bus_stop not in bus_stop_visited: queue.append(bus_stop) bus_stop_visited.add(bus_stop) bus_visited.add(bus) level += 1 return -1 if __name__ == '__main__': obj = Innoskrit() routes = [[1, 2, 3], [1, 4, 5], [5, 8, 7], [3, 6, 7]] print(obj.numBusesToDestination(routes, 1, 7))
Output:
2