https://github.com/emahtab/bus-routes
https://github.com/emahtab/bus-routes
breadth-first-search leetcode
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/emahtab/bus-routes
- Owner: eMahtab
- Created: 2021-09-18T04:36:04.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2021-09-18T04:45:35.000Z (almost 4 years ago)
- Last Synced: 2025-02-02T03:26:16.704Z (5 months ago)
- Topics: breadth-first-search, leetcode
- Homepage:
- Size: 6.84 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Bus Routes
## https://leetcode.com/problems/bus-routesYou 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 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
```### BFS Implementation 1 : Time Limit Exceeded
```java
class Solution {
public int numBusesToDestination(int[][] routes, int source, int target) {
if(routes == null || routes.length == 0)
return 0;
Map> stopToRouteMap = new HashMap<>();
for(int i = 0; i < routes.length; i++) {
for(int j = 0; j < routes[i].length; j++) {
stopToRouteMap.putIfAbsent(routes[i][j], new ArrayList());
stopToRouteMap.get(routes[i][j]).add(i);
}
}
int busChange = 0;
Queue q = new ArrayDeque<>();
q.add(source);
Set stopSeen = new HashSet<>();
stopSeen.add(source);
while(!q.isEmpty()) {
int size = q.size();
for(int i = 0; i < size; i++) {
int stop = q.remove();
if(stop == target)
return busChange;
List busRoutes = stopToRouteMap.get(stop);
for(int routeNumber : busRoutes) {
for(int newStop : routes[routeNumber]) {
if(!stopSeen.contains(newStop)) {
q.add(newStop);
stopSeen.add(newStop);
}
}
}
}
busChange++;
}
return -1;
}
}
```### BFS Implementation 2 : Optimized
```java
class Solution {
public int numBusesToDestination(int[][] routes, int source, int target) {
if(routes == null || routes.length == 0)
return 0;
Map> stopToRouteMap = new HashMap<>();
for(int i = 0; i < routes.length; i++) {
for(int j = 0; j < routes[i].length; j++) {
stopToRouteMap.putIfAbsent(routes[i][j], new ArrayList());
stopToRouteMap.get(routes[i][j]).add(i);
}
}
int busChange = 0;
Queue q = new ArrayDeque<>();
q.add(source);
Set stopSeen = new HashSet<>();
stopSeen.add(source);
Set routeSeen = new HashSet<>();
while(!q.isEmpty()) {
int size = q.size();
for(int i = 0; i < size; i++) {
int stop = q.remove();
if(stop == target)
return busChange;
List busRoutes = stopToRouteMap.get(stop);
for(int routeNumber : busRoutes) {
if(!routeSeen.contains(routeNumber)) {
for(int newStop : routes[routeNumber]) {
if(!stopSeen.contains(newStop)) {
q.add(newStop);
stopSeen.add(newStop);
}
}
routeSeen.add(routeNumber);
}
}
}
busChange++;
}
return -1;
}
}
```### References :
https://www.youtube.com/watch?v=odmGyOJM5EY (Nice Video)