The Number of Passengers in Each Bus I Problem
Description
LeetCode Problem 2142.
Table: Buses
+--------------+------+
| Column Name | Type |
+--------------+------+
| bus_id | int |
| arrival_time | int |
+--------------+------+
bus_id is the column with unique values for this table.
Each row of this table contains information about the arrival time of a bus at the LeetCode station.
No two buses will arrive at the same time.
Table: Passengers
+--------------+------+
| Column Name | Type |
+--------------+------+
| passenger_id | int |
| arrival_time | int |
+--------------+------+
passenger_id is the column with unique values for this table.
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 time t_bus and a passenger arrived at time t_passenger where t_passenger <= t_bus and the passenger did not catch any bus, the passenger will use that 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 |
+--------+--------------+
| 1 | 2 |
| 2 | 4 |
| 3 | 7 |
+--------+--------------+
Passengers table:
+--------------+--------------+
| passenger_id | arrival_time |
+--------------+--------------+
| 11 | 1 |
| 12 | 5 |
| 13 | 6 |
| 14 | 7 |
+--------------+--------------+
Output:
+--------+----------------+
| bus_id | passengers_cnt |
+--------+----------------+
| 1 | 1 |
| 2 | 0 |
| 3 | 3 |
+--------+----------------+
Explanation:
- Passenger 11 arrives at time 1.
- Bus 1 arrives at time 2 and collects passenger 11.
- Bus 2 arrives at time 4 and does not collect any passengers.
- 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, 13, and 14.
Solution
Approach: Join over the interval since the previous bus
A passenger takes the first bus that arrives at or after them, so bus i carries exactly the passengers who arrived after the previous bus and no later than bus i. Order the buses by arrival time and use LAG to get each bus's predecessor time (NULL for the first bus, which then has no lower bound). A LEFT JOIN onto Passengers over the interval (previous, current] followed by COUNT(passenger_id) gives the number per bus, and buses nobody boards still show up with 0.
Note that bus_id need not follow arrival order, which is why the predecessor is found with ORDER BY arrival_time and the final result is sorted by bus_id separately.
WITH bus AS (
SELECT bus_id,
arrival_time,
LAG(arrival_time) OVER (ORDER BY arrival_time) AS prev_time
FROM Buses
)
SELECT b.bus_id,
COUNT(p.passenger_id) AS passengers_cnt
FROM bus AS b
LEFT JOIN Passengers AS p
ON p.arrival_time <= b.arrival_time
AND (b.prev_time IS NULL OR p.arrival_time > b.prev_time)
GROUP BY b.bus_id
ORDER BY b.bus_id;