You are given a 0-indexed integer array buses of length n, where buses[i] represents the departure time of the ith bus. You are also given a 0-indexed integer array passengers of length m, where passengers[j] represents the arrival time of the jth passenger. All bus departure times are unique. All passenger arrival times are unique.
You are given an integer capacity, which represents the maximum number of passengers that can get on each bus.
When a passenger arrives, they will wait in line for the next available bus. You can get on a bus that departs at x minutes if you arrive at y minutes where y <= x, and the bus is not full. Passengers with the earliest arrival times get on the bus first.
More formally when a bus arrives, either:
capacity or fewer passengers are waiting for a bus, they will all get on the bus, orcapacity passengers with the earliest arrival times will get on the bus.Return the latest time you may arrive at the bus station to catch a bus. You cannot arrive at the same time as another passenger.
Note: The arrays buses and passengers are not necessarily sorted.
Example 1:
Input: buses = [10,20], passengers = [2,17,18,19], capacity = 2 Output: 16 Explanation: Suppose you arrive at time 16. At time 10, the first bus departs with the 0th passenger. At time 20, the second bus departs with you and the 1st passenger. Note that you may not arrive at the same time as another passenger, which is why you must arrive before the 1st passenger to catch the bus.
Example 2:
Input: buses = [20,30,10], passengers = [19,13,26,4,25,11,21], capacity = 2 Output: 20 Explanation: Suppose you arrive at time 20. At time 10, the first bus departs with the 3rd passenger. At time 20, the second bus departs with the 5th and 1st passengers. At time 30, the third bus departs with the 0th passenger and you. Notice if you had arrived any later, then the 6th passenger would have taken your seat on the third bus.
Constraints:
n == buses.lengthm == passengers.length1 <= n, m, capacity <= 1052 <= buses[i], passengers[i] <= 109buses is unique.passengers is unique.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We need to figure out the very last moment someone can arrive at a bus stop and still catch a bus. The brute force way is like trying every possible arrival time, one by one, to see if it works. We check each time to ensure the person can catch a bus without delaying anyone else.
Here's how the algorithm would work step-by-step:
def the_latest_time_to_catch_the_bus(buses, passengers, bus_capacity):
buses.sort()
passengers.sort()
latest_possible_time = buses[-1]
for arrival_time in range(latest_possible_time, 0, -1):
temp_passengers = passengers[:]
bus_index = 0
passenger_index = 0
passengers_on_bus = 0
# Simulate the bus arrival and passenger boarding process
while bus_index < len(buses) and passenger_index < len(temp_passengers):
if temp_passengers[passenger_index] <= buses[bus_index]:
passengers_on_bus += 1
passenger_index += 1
else:
bus_index += 1
passengers_on_bus = 0
if passengers_on_bus == bus_capacity:
bus_index += 1
passengers_on_bus = 0
# Check if the new arrival can board a bus
bus_index = 0
passenger_index = 0
passengers_on_bus = 0
while bus_index < len(buses) and passenger_index < len(temp_passengers):
if temp_passengers[passenger_index] <= buses[bus_index]:
if temp_passengers[passenger_index] < arrival_time:
passengers_on_bus += 1
else:
break
passenger_index += 1
else:
bus_index += 1
passengers_on_bus = 0
if passengers_on_bus == bus_capacity:
bus_index += 1
passengers_on_bus = 0
# Check if the arrival time is valid.
can_board = True
bus_index = 0
passenger_index = 0
passengers_on_bus = 0
while bus_index < len(buses) and passenger_index < len(temp_passengers):
if temp_passengers[passenger_index] <= buses[bus_index]:
passengers_on_bus += 1
if temp_passengers[passenger_index] == arrival_time:
can_board = False
break
passenger_index += 1
else:
bus_index += 1
passengers_on_bus = 0
if passengers_on_bus == bus_capacity:
bus_index += 1
passengers_on_bus = 0
if not can_board:
continue
bus_index = 0
passenger_index = 0
passengers_on_bus = 0
while bus_index < len(buses) and passenger_index < len(temp_passengers):
if temp_passengers[passenger_index] <= buses[bus_index]:
passengers_on_bus += 1
passenger_index += 1
else:
bus_index += 1
passengers_on_bus = 0
if passengers_on_bus == bus_capacity:
bus_index += 1
passengers_on_bus = 0
# Check if someone is already arriving at that time.
if arrival_time not in passengers:
# Ensures we don't return duplicates, since we decrement the arrival time
return arrival_time
# Try one second earlier to avoid collision with existing passengers
arrival_time -= 1
# Check if an earlier time is still valid
temp_passengers = passengers[:]
bus_index = 0
passenger_index = 0
passengers_on_bus = 0
while bus_index < len(buses) and passenger_index < len(temp_passengers):
if temp_passengers[passenger_index] <= buses[bus_index]:
passengers_on_bus += 1
passenger_index += 1
else:
bus_index += 1
passengers_on_bus = 0
if passengers_on_bus == bus_capacity:
bus_index += 1
passengers_on_bus = 0
# Check again if the arrival time is valid.
can_board = True
bus_index = 0
passenger_index = 0
passengers_on_bus = 0
while bus_index < len(buses) and passenger_index < len(temp_passengers):
if temp_passengers[passenger_index] <= buses[bus_index]:
passengers_on_bus += 1
if temp_passengers[passenger_index] == arrival_time:
can_board = False
break
passenger_index += 1
else:
bus_index += 1
passengers_on_bus = 0
if passengers_on_bus == bus_capacity:
bus_index += 1
passengers_on_bus = 0
if not can_board:
continue
bus_index = 0
passenger_index = 0
passengers_on_bus = 0
while bus_index < len(buses) and passenger_index < len(temp_passengers):
if temp_passengers[passenger_index] <= buses[bus_index]:
passengers_on_bus += 1
passenger_index += 1
else:
bus_index += 1
passengers_on_bus = 0
if passengers_on_bus == bus_capacity:
bus_index += 1
passengers_on_bus = 0
# Try one second earlier to avoid collision with existing passengers
return arrival_time
return 0The key is to first ensure everything is in order, making it easier to find the best possible time. Then, work backward from the latest bus, trying to board passengers at the latest possible moment without conflict.
Here's how the algorithm would work step-by-step:
def the_latest_time_to_catch_the_bus(buses, passengers, bus_capacity):
buses.sort()
passengers.sort()
passenger_index = 0
available_times = set()
for bus_time in buses:
current_capacity = 0
# Prioritize passengers who can board later buses.
for i in range(len(passengers) - 1, -1, -1):
if passengers[i] <= bus_time and passengers[i] not in available_times and current_capacity < bus_capacity:
#Try to find a time one second before the passenger's arrival
time_to_board = passengers[i]
while time_to_board in available_times:
time_to_board -= 1
if time_to_board == 0:
break
if time_to_board > 0:
available_times.add(time_to_board)
current_capacity += 1
if not available_times:
return buses[-1]
latest_possible_time = max(available_times)
# Ensure the returned time is not a passenger arrival time.
while latest_possible_time in set(passengers):
latest_possible_time -= 1
return latest_possible_time| Case | How to Handle |
|---|---|
| Empty buses or passengers array | If either array is empty, no passenger can catch a bus, return null/appropriate error value. |
| buses or passengers array with a single element | Handle the single element array according to the problem rules. |
| All buses arrive before all passengers | Return the latest time a passenger can arrive before the first bus if possible; otherwise, return the first bus arrival time minus one if there's no earlier valid time. |
| All passengers arrive before any bus | Return the latest possible arrival time (considering duplicates) before the first bus. |
| Maximum possible integer value in input | Consider Integer overflow if arrival times are summed up and ensure the result remains within the valid integer range. |
| Duplicate bus arrival times | The solution should account for duplicate bus arrival times when considering which bus a passenger can take. |
| Duplicate passenger arrival times | Handle the case where multiple passengers arrive at the same time, ensuring the solution returns the latest possible arrival time. |
| No passenger can catch any bus | Determine the latest possible time before the first bus arrives if no passenger can catch a bus according to problem constraints; otherwise, there might be an error in input. |