The Latest Time to Catch a Bus - Problem
The Latest Time to Catch a Bus
Imagine you're at a busy bus station where multiple buses depart throughout the day, each with limited seating capacity. You want to arrive as late as possible while still guaranteeing you can catch a bus!
You're given:
•
•
•
Rules:
1. You can board a bus if you arrive before or exactly when it departs
2. Buses fill up in first-come, first-served order
3. You cannot arrive at the same time as another passenger
4. If a bus is full, you must wait for the next one
Goal: Find the latest possible arrival time that still guarantees you catch a bus.
Imagine you're at a busy bus station where multiple buses depart throughout the day, each with limited seating capacity. You want to arrive as late as possible while still guaranteeing you can catch a bus!
You're given:
•
buses[i] - departure times of buses (not necessarily sorted)•
passengers[j] - arrival times of existing passengers (not necessarily sorted)•
capacity - maximum passengers per busRules:
1. You can board a bus if you arrive before or exactly when it departs
2. Buses fill up in first-come, first-served order
3. You cannot arrive at the same time as another passenger
4. If a bus is full, you must wait for the next one
Goal: Find the latest possible arrival time that still guarantees you catch a bus.
Input & Output
example_1.py — Basic Case
$
Input:
buses = [3], passengers = [2], capacity = 2
›
Output:
3
💡 Note:
Only one bus at time 3 with capacity 2. One passenger arrives at time 2. We can arrive at time 3 (same as bus departure) and still board since there's space.
example_2.py — Multiple Buses
$
Input:
buses = [2,4], passengers = [1,3], capacity = 1
›
Output:
3
💡 Note:
Bus 1 (t=2, cap=1): passenger at t=1 boards. Bus 2 (t=4, cap=1): passenger at t=3 boards. No space on either bus if we arrive at existing passenger times. But we can arrive at t=3-1=2, but that conflicts with Bus 1's time. So we need to be more strategic - actually we can arrive at t=4 but passenger at t=3 takes the spot, so we arrive at t=2.
example_3.py — Edge Case
$
Input:
buses = [5], passengers = [2,4], capacity = 3
›
Output:
5
💡 Note:
Bus at time 5 with capacity 3. Two passengers arrive at times 2 and 4. Bus has space for one more, so we can arrive exactly at time 5 (bus departure time).
Visualization
Tap to expand
Understanding the Visualization
1
Study the Schedule
Sort all bus departure times and passenger arrival times to understand the boarding order
2
Simulate Rush Hour
Watch how passengers fill each bus in first-come-first-served order until capacity is reached
3
Work Backwards
Start from the latest bus and check: is there space, or can you 'cut in line' before another passenger?
4
Find Your Moment
Either arrive exactly at bus departure (if space) or 1 minute before the passenger you'd replace
Key Takeaway
🎯 Key Insight: Work backwards from the latest bus. Either arrive exactly at departure time (if space available) or arrive 1 minute before the passenger you'd need to replace. This greedy approach guarantees the latest possible arrival time!
Time & Space Complexity
Time Complexity
O(T × (n + m))
T is the range of possible arrival times, n is buses, m is passengers. For each time T, we simulate boarding process.
✓ Linear Growth
Space Complexity
O(n + m)
Space for sorted copies of buses and passengers arrays
⚡ Linearithmic Space
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code