The Number of Passengers in Each Bus II - Problem
Welcome to the LeetCode Bus Station! ๐
You're managing a busy transit hub where buses and passengers arrive throughout the day. Your job is to efficiently match passengers with buses based on their arrival times and bus capacities.
๐ The Rules:
- ๐ Buses arrive with specific capacities (number of empty seats)
- ๐งโ๐คโ๐ง Passengers arrive at various times
- โฐ A passenger can board a bus only if the passenger arrived before or at the same time as the bus
- ๐ฏ Passengers board the earliest available bus they're eligible for
- ๐ข If more passengers are waiting than the bus capacity, only
capacitypassengers can board
Your mission: Calculate how many passengers boarded each bus and return the results ordered by bus_id.
Think of it as a real-time passenger allocation system where timing and capacity constraints determine the boarding process!
Input & Output
example_1.sql โ Basic Case
$
Input:
Buses:
| bus_id | arrival_time | capacity |
|--------|--------------|----------|
| 1 | 4 | 2 |
| 2 | 5 | 3 |
| 3 | 6 | 2 |
Passengers:
| passenger_id | arrival_time |
|-------------|---------------|
| 1 | 2 |
| 2 | 1 |
| 3 | 5 |
| 4 | 3 |
โบ
Output:
| bus_id | passengers_cnt |
|--------|----------------|
| 1 | 2 |
| 2 | 2 |
| 3 | 0 |
๐ก Note:
Bus 1 (t=4): Passengers 1,2,4 are eligible (arrived โค4). Capacity=2, so passengers 2,1 board (earliest arrivals). Bus 2 (t=5): Passengers 4,3 remain available. Both board. Bus 3 (t=6): No passengers left.
example_2.sql โ High Capacity
$
Input:
Buses:
| bus_id | arrival_time | capacity |
|--------|--------------|----------|
| 1 | 3 | 10 |
Passengers:
| passenger_id | arrival_time |
|-------------|---------------|
| 1 | 1 |
| 2 | 2 |
| 3 | 4 |
โบ
Output:
| bus_id | passengers_cnt |
|--------|----------------|
| 1 | 2 |
๐ก Note:
Bus 1 arrives at t=3. Passengers 1(t=1) and 2(t=2) are eligible since they arrived before the bus. Passenger 3(t=4) arrived after the bus, so cannot board. Result: 2 passengers board bus 1.
example_3.sql โ Empty Buses
$
Input:
Buses:
| bus_id | arrival_time | capacity |
|--------|--------------|----------|
| 1 | 2 | 5 |
| 2 | 4 | 3 |
Passengers:
| passenger_id | arrival_time |
|-------------|---------------|
| 1 | 5 |
| 2 | 6 |
โบ
Output:
| bus_id | passengers_cnt |
|--------|----------------|
| 1 | 0 |
| 2 | 0 |
๐ก Note:
All passengers arrive after all buses have departed. Bus 1 arrives at t=2, Bus 2 at t=4, but passengers arrive at t=5 and t=6. Since passengers must arrive before or at the same time as the bus, no one can board any bus.
Visualization
Tap to expand
Understanding the Visualization
1
Timeline Setup
Arrange buses and passengers on a timeline by their arrival times
2
Eligibility Check
For each bus, identify passengers who arrived before or at bus time
3
First Bus Assignment
Assign each passenger to their earliest eligible bus
4
Capacity Control
Apply capacity limits - excess passengers wait for next bus
5
Final Tally
Count passengers assigned to each bus
Key Takeaway
๐ฏ Key Insight: Use window functions to efficiently determine each passenger's first eligible bus, then apply capacity constraints. This avoids the need for complex nested loops and provides optimal O(B log B + P log P) performance.
Time & Space Complexity
Time Complexity
O(B log B + P log P)
Sorting buses and passengers, then single linear pass
โก Linearithmic
Space Complexity
O(B + P)
Only need to store final results, not intermediate cross-products
โ Linear Space
Constraints
- 1 โค buses.length, passengers.length โค 105
- buses[i].length == 3, passengers[i].length == 2
- 1 โค bus_id, passenger_id โค 106
- 1 โค arrival_time โค 109
- 1 โค capacity โค 105
- All bus_id and passenger_id values are unique
- No two buses arrive at the same time
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code