The Number of Passengers in Each Bus II Problem
Description
LeetCode Problem 2153.
Table: Buses
+--------------+------+
| Column Name | Type |
+--------------+------+
| bus_id | int |
| arrival_time | int |
| capacity | int |
+--------------+------+
bus_id contains unique values.
Each row of this table contains information about the arrival time of a bus at the LeetCode station and its capacity (the number of empty seats it has).
No two buses will arrive at the same time and all bus capacities will be positive integers.
Table: Passengers
+--------------+------+
| Column Name | Type |
+--------------+------+
| passenger_id | int |
| arrival_time | int |
+--------------+------+
passenger_id contains unique values.
Each row of this table contains information about the arrival time of a passenger at the LeetCode station.
Buses and passengers arrive at the LeetCode station. If a bus arrives at the station at a time t_bus and a passenger arrived at a time t_passenger where t_passenger <= t_bus and the passenger did not catch any bus, the passenger will use that bus. In addition, each bus has a capacity. If at the moment the bus arrives at the station there are more passengers waiting than its capacity capacity, only capacity passengers will use the bus.
Write a solution to report the number of users that used each bus.
Return the result table ordered by bus_id in ascending order.
The result format is in the following example.
Example 1:
Input:
Buses table:
+--------+--------------+----------+
| bus_id | arrival_time | capacity |
+--------+--------------+----------+
| 1 | 2 | 1 |
| 2 | 4 | 10 |
| 3 | 7 | 2 |
+--------+--------------+----------+
Passengers table:
+--------------+--------------+
| passenger_id | arrival_time |
+--------------+--------------+
| 11 | 1 |
| 12 | 1 |
| 13 | 5 |
| 14 | 6 |
| 15 | 7 |
+--------------+--------------+
Output:
+--------+----------------+
| bus_id | passengers_cnt |
+--------+----------------+
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
+--------+----------------+
Explanation:
- Passenger 11 arrives at time 1.
- Passenger 12 arrives at time 1.
- Bus 1 arrives at time 2 and collects passenger 11 as it has one empty seat.
- Bus 2 arrives at time 4 and collects passenger 12 as it has ten empty seats.
- Passenger 12 arrives at time 5.
- Passenger 13 arrives at time 6.
- Passenger 14 arrives at time 7.
- Bus 3 arrives at time 7 and collects passengers 12 and 13 as it has two empty seats.
Solution
Approach: Recursive simulation
Simulate the buses in arrival order. For a bus, let arrived be the number of passengers who have arrived by its time (a count over Passengers). Everyone who already boarded an earlier bus is gone, so the people waiting are arrived - boarded_so_far, and the bus takes LEAST(capacity, waiting) of them. Each step depends on the previous step's running total, which is a natural fit for a recursive CTE: it starts at the first bus (rn = 1) and moves to bus rn + 1, carrying total, the number of passengers boarded so far.
Leftover passengers who did not fit stay in the queue, because total only grows by the number that actually boarded.
WITH RECURSIVE bus AS (
SELECT bus_id,
capacity,
ROW_NUMBER() OVER (ORDER BY arrival_time) AS rn,
(SELECT COUNT(*) FROM Passengers AS p
WHERE p.arrival_time <= Buses.arrival_time) AS arrived
FROM Buses
),
sim AS (
SELECT rn,
bus_id,
LEAST(capacity, arrived) AS cnt,
LEAST(capacity, arrived) AS total
FROM bus
WHERE rn = 1
UNION ALL
SELECT b.rn,
b.bus_id,
LEAST(b.capacity, b.arrived - s.total),
s.total + LEAST(b.capacity, b.arrived - s.total)
FROM sim AS s
JOIN bus AS b ON b.rn = s.rn + 1
)
SELECT bus_id, cnt AS passengers_cnt
FROM sim
ORDER BY bus_id;