The Number of Passengers in Each Bus I - Problem
The Number of Passengers in Each Bus I

Welcome to the LeetCode Transit Authority! ๐ŸšŒ You're tasked with analyzing passenger boarding data at our busy station.

You have two tables:

Buses Table:
+--------------+------+
| Column Name | Type |
+--------------+------+
| bus_id | int |
| arrival_time | int |
+--------------+------+


Passengers Table:
+--------------+------+
| Column Name | Type |
+--------------+------+
| passenger_id | int |
| arrival_time | int |
+--------------+------+


Boarding Rules:
โ€ข Passengers board the first available bus that arrives at or after their arrival time
โ€ข Once a passenger boards a bus, they cannot board another
โ€ข No two buses or passengers arrive at the exact same time

Your Mission: Calculate how many passengers board each bus and return results ordered by bus_id ascending.

Think of it like a real transit system - passengers wait at the station and hop on the first bus that can take them to their destination!

Input & Output

Basic Example - Sequential Arrival
$ Input: Buses: [(bus_id=1, arrival_time=2), (bus_id=2, arrival_time=4)]\nPassengers: [(passenger_id=1, arrival_time=1), (passenger_id=2, arrival_time=3)]
โ€บ Output: [(bus_id=1, passengers_cnt=1), (bus_id=2, passengers_cnt=1)]
๐Ÿ’ก Note: Passenger 1 arrives at t=1 and boards Bus 1 (arrives t=2). Passenger 2 arrives at t=3 and boards Bus 2 (arrives t=4) since Bus 1 already left.
Multiple Passengers Same Bus
$ Input: Buses: [(bus_id=1, arrival_time=5)]\nPassengers: [(passenger_id=1, arrival_time=1), (passenger_id=2, arrival_time=3), (passenger_id=3, arrival_time=5)]
โ€บ Output: [(bus_id=1, passengers_cnt=3)]
๐Ÿ’ก Note: All three passengers arrive before or at t=5, so they all board Bus 1 which is the first (and only) bus that can take them.
Edge Case - No Passengers Can Board
$ Input: Buses: [(bus_id=1, arrival_time=1)]\nPassengers: [(passenger_id=1, arrival_time=2)]
โ€บ Output: [(bus_id=1, passengers_cnt=0)]
๐Ÿ’ก Note: Passenger 1 arrives at t=2 but Bus 1 already left at t=1, so no passengers board Bus 1.

Constraints

  • 1 โ‰ค buses.length, passengers.length โ‰ค 103
  • 1 โ‰ค bus_id, passenger_id โ‰ค 106
  • 1 โ‰ค arrival_time โ‰ค 109
  • No two buses arrive at the same time
  • No two passengers arrive at the same time

Visualization

Tap to expand
๐ŸšŒ Bus Station Passenger Assignment SystemTimeline (sorted by arrival)P1t=1Bus1t=2P2t=3Bus2t=4P3t=5Bus3t=6boardsboardsboardsAlgorithm Steps:1. Sort: Buses [Bus1(t=2), Bus2(t=4), Bus3(t=6)] | Passengers [P1(t=1), P2(t=3), P3(t=5)]2. Two Pointers: Bus pointer starts at Bus1, Passenger pointer starts at P13. Process: For each bus, count passengers who can board (arrived โ‰ค bus time)4. Assign: P1โ†’Bus1 (1 passenger), P2โ†’Bus2 (1 passenger), P3โ†’Bus3 (1 passenger)5. Result: Bus1: 1 passenger, Bus2: 1 passenger, Bus3: 1 passengerโŒ Brute Force: O(nร—m)Check every passengeragainst every busVery slow for large datasetsโœ… Two Pointers: O(n log n)Single pass after sortingEach element processed onceOptimal for large datasets
Understanding the Visualization
1
Sort Schedules
Organize buses and passengers by arrival times for efficient processing
2
Two-Pointer Sweep
Use two pointers to traverse sorted arrays and match passengers to buses
3
First-Come-First-Bus
Ensure each passenger boards the earliest available bus they can catch
Key Takeaway
๐ŸŽฏ Key Insight: Sort both arrays and use two pointers to process them in chronological order, ensuring each passenger boards the earliest available bus in optimal O(n log n) time.
Asked in
Amazon 35 Google 28 Microsoft 22 Meta 18
28.6K Views
Medium Frequency
~15 min Avg. Time
842 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