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:
Passengers Table:
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
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!
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
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.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code