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 capacity passengers 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
๐ŸšŒ Bus Station Timeline SimulationTimeline (arrival times)t=1t=2t=3t=4t=5t=6P2t=1P1t=2P4t=3P3t=5Bus1t=4cap=2Eligible: P2,P1,P4Takes: P2,P1 (first 2)Bus2t=5cap=3Available: P4,P3Takes: P4,P3 (both)Bus3t=6cap=2Available: NoneTakes: 0๐ŸŽฏ Assignment Resultsโœ… Bus 1: 2 passengers (P2, P1)โœ… Bus 2: 2 passengers (P4, P3)โŒ Bus 3: 0 passengers (none available)Passengers board the earliest bus they're eligible for!
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

n
2n
โšก Linearithmic
Space Complexity
O(B + P)

Only need to store final results, not intermediate cross-products

n
2n
โœ“ 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
Asked in
Google 45 Amazon 38 Meta 25 Microsoft 22 Apple 18 Netflix 12
43.7K Views
Medium-High Frequency
~35 min Avg. Time
1.8K 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