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: 20
💡 Note: After sorting: Bus 10 takes [4,11], Bus 20 takes [13,19], Bus 30 takes [21,25]. Bus 20 has space, so we can arrive exactly at time 20 (but 19 conflicts, so 18).
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
Latest Time to Catch a Bus - Problem OverviewBus 10Capacity: 2Bus 20Capacity: 2Passengers:2Passengers:1718FULL!Strategy:1. Bus 20 is full with [17,18]2. Arrive at 16 (just before 17)3. Displace passenger 174. Board bus 20 successfully!YouArrive at time 16Latest Possible Arrival: 16
Understanding the Visualization
1
Input
Buses [10,20], Passengers [2,17,18,19], Capacity 2
2
Simulate
Bus 10: passenger 2 boards, Bus 20: passengers 17,18 board
3
Optimize
Arrive at 16 to displace passenger 17 and catch bus 20
Key Takeaway
🎯 Key Insight: Either arrive at the bus departure time if there's space, or arrive just before another passenger to displace them
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