{x}
blog image

The Latest Time to Catch a Bus

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:

  • If capacity or fewer passengers are waiting for a bus, they will all get on the bus, or
  • The capacity 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.length
  • m == passengers.length
  • 1 <= n, m, capacity <= 105
  • 2 <= buses[i], passengers[i] <= 109
  • Each element in buses is unique.
  • Each element in passengers is unique.

Solution Explanation: The Latest Time to Catch a Bus

This problem asks to find the latest time a passenger can arrive at the bus station and still catch a bus. The solution involves sorting, simulating bus departures, and a bit of clever post-processing.

Approach:

  1. Sort: We start by sorting both the buses and passengers arrays. This allows us to efficiently process passengers in arrival order and check for bus availability.

  2. Simulation: We iterate through the sorted buses array. For each bus, we simulate passengers boarding. We maintain a capacity counter representing the remaining seats on the bus. We iterate through the passengers array using a pointer j. If a passenger arrives before the bus departs (passengers[j] <= t, where t is the current bus departure time) and there's space on the bus (c > 0), the passenger boards, capacity decreases, and we move to the next passenger.

  3. Determining the Latest Arrival Time: After simulating all bus departures, we need to determine the latest possible arrival time.

    • If there are remaining seats on the last bus (c > 0): The latest arrival time is the departure time of the last bus (buses[-1]). However, we need to adjust this if another passenger arrives at the same time. We decrement the time until we find a time where no passenger arrives.

    • If the last bus is full (c == 0): The latest arrival time is the arrival time of the last passenger who boarded the last bus (passengers[j]). Again, we need to adjust this to ensure we don't arrive at the same time as another passenger. We decrement the time until we find a time where no passenger arrives.

Time Complexity:

  • Sorting the buses and passengers arrays takes O(n log n) and O(m log m) time respectively, where n is the number of buses and m is the number of passengers.
  • The simulation loop iterates through the buses once, which takes O(n) time. Within the loop, we might iterate through all passengers in the worst case, which takes O(m) time.
  • The post-processing to adjust the arrival time takes at most O(m) time in the worst case.

Therefore, the overall time complexity is dominated by sorting and is O(n log n + m log m).

Space Complexity:

The space complexity is determined by the space used for sorting (which can vary depending on the sorting algorithm used, but is often O(log n) and O(log m) in-place algorithms), and any auxiliary variables used during the simulation. This results in a space complexity of O(log n + log m) (or O(1) if using in-place sorting).

Code Examples (Python):

class Solution:
    def latestTimeCatchTheBus(self, buses: List[int], passengers: List[int], capacity: int) -> int:
        buses.sort()
        passengers.sort()
        n = len(buses)
        m = len(passengers)
        j = 0
        for i in range(n):
            count = capacity
            while j < m and count > 0 and passengers[j] <= buses[i]:
                count -= 1
                j += 1
 
        j -= 1  # Adjust j to point to the last passenger on the last bus
 
        latest_time = buses[-1] if count > 0 else passengers[j]
 
        while j >= 0 and passengers[j] == latest_time:
            latest_time -= 1
            j -= 1
 
        return latest_time

This Python code directly implements the algorithm described above. The other language examples follow the same logic. Note that minor variations might exist across implementations due to differences in how sorting and array handling is performed in each language.