The Latest Time to Catch a Bus - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: buses = [10,20], passengers = [2,17,18,19], capacity = 2
Output: 16
💡 Note: Bus 10: passengers 2 boards. Bus 20: passengers 17,18 board (full). We can arrive at 16, displace passenger 17, and catch bus 20.
Example 2 — Space Available
$ Input: buses = [20,30,10], passengers = [19,13,26,4,25,11,21], capacity = 2
Output: 24
💡 Note: After sorting: Bus 10 takes [4,11], Bus 20 takes [13,19], Bus 30 takes [21,25]. All buses are full. To catch a bus, we work backwards: Bus 30 is full with latest passenger 25, so we arrive at time 24 to displace passenger 25 and catch Bus 30.
Example 3 — Single Bus
$ Input: buses = [3], passengers = [2], capacity = 2
Output: 3
💡 Note: Bus 3 takes passenger 2 and has space for 1 more. We can arrive at time 3 and catch the 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

Visualization

Tap to expand
The Latest Time to Catch a Bus INPUT buses = [10, 20] Bus 1 t=10 Bus 2 t=20 passengers = [2, 17, 18, 19] 2 17 18 19 capacity = 2 2 Timeline: 2 10 17 18 19 20 ALGORITHM STEPS 1 Sort Arrays Sort buses and passengers 2 Simulate Boarding Track who boards each bus Bus Boards Spaces t=10 [2] 1 left t=20 [17,18] FULL 3 Find Last Position Check last bus capacity 4 Avoid Collisions Skip taken times: 18,17 19: taken 18: taken 17: taken 16: OK FINAL RESULT Latest Time: 16 Why 16? Bus 2 (t=20) is full with [17,18] Passenger 19 misses the bus! We can replace passenger 18: - Time 18: already taken - Time 17: already taken - Time 16: AVAILABLE! Final Boarding: Bus 2 (t=20) [16, 17] You board at t=16! Key Insight: The optimal strategy is to either: (1) take the last spot on the last bus if space exists, or (2) arrive just before the last person who boarded the last bus. We must avoid times already taken by other passengers, so we decrement until finding an available slot (18 taken, 17 taken, 16 available). TutorialsPoint - The Latest Time to Catch a Bus | Optimal Solution
Asked in
Microsoft 15 Google 12 Amazon 8 Meta 6
12.5K Views
Medium Frequency
~35 min Avg. Time
486 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen