LeetCode 2153: The Number of Passengers in Each Bus II Solution
Master LeetCode problem 2153 (The Number of Passengers in Each Bus II), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
2153. The Number of Passengers in Each Bus II
Problem Explanation
Explanation:
To solve this problem, we can use a binary search approach. We can define the search space as the range of possible values for the number of passengers in each bus. Then, we can perform a binary search within this search space to find the minimum number of passengers such that all passengers can be transported within m buses and t time units.
Here are the steps:
- Define the search space for the number of passengers in each bus as
[1, sum(passenger times)]. - Perform a binary search within this search space.
- For each mid value, simulate the process of transporting passengers in
mbuses and check if it can be done withinttime units. - Adjust the search space based on the result of the simulation.
- Repeat the binary search until we find the minimum number of passengers.
Time complexity: O(n log(sum(passenger times))), where n is the number of passengers. Space complexity: O(1)
: :
Solution Code
class Solution {
public int numBusesToDestination(int[][] routes, int source, int target) {
int n = routes.length;
Map<Integer, Set<Integer>> stopToBuses = new HashMap<>();
for (int bus = 0; bus < n; bus++) {
for (int stop : routes[bus]) {
if (!stopToBuses.containsKey(stop)) {
stopToBuses.put(stop, new HashSet<>());
}
stopToBuses.get(stop).add(bus);
}
}
Set<Integer> visitedBuses = new HashSet<>();
Set<Integer> visitedStops = new HashSet<>();
Queue<Integer> queue = new LinkedList<>();
queue.offer(source);
int level = 0;
while (!queue.isEmpty()) {
int size = queue.size();
level++;
for (int i = 0; i < size; i++) {
int currStop = queue.poll();
if (currStop == target) {
return level - 1;
}
for (int bus : stopToBuses.get(currStop)) {
if (visitedBuses.contains(bus)) {
continue;
}
visitedBuses.add(bus);
for (int nextStop : routes[bus]) {
if (!visitedStops.contains(nextStop)) {
visitedStops.add(nextStop);
queue.offer(nextStop);
}
}
}
}
}
return -1;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 2153 (The Number of Passengers in Each Bus II)?
This page provides optimized solutions for LeetCode problem 2153 (The Number of Passengers in Each Bus II) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 2153 (The Number of Passengers in Each Bus II)?
The time complexity for LeetCode 2153 (The Number of Passengers in Each Bus II) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 2153 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2153 in Java, C++, or Python.