-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2153-TheNumberOfPassengersInEachBusII.sql
More file actions
173 lines (164 loc) · 5.85 KB
/
2153-TheNumberOfPassengersInEachBusII.sql
File metadata and controls
173 lines (164 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
-- 2153. The Number of Passengers in Each Bus II
-- 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 tbus and a passenger arrived at a time tpassenger where tpassenger <= tbus 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.
-- Create table If Not Exists Buses (bus_id int, arrival_time int, capacity int)
-- Create table If Not Exists Passengers (passenger_id int, arrival_time int)
-- Truncate table Buses
-- insert into Buses (bus_id, arrival_time, capacity) values ('1', '2', '1')
-- insert into Buses (bus_id, arrival_time, capacity) values ('2', '4', '10')
-- insert into Buses (bus_id, arrival_time, capacity) values ('3', '7', '2')
-- Truncate table Passengers
-- insert into Passengers (passenger_id, arrival_time) values ('11', '1')
-- insert into Passengers (passenger_id, arrival_time) values ('12', '1')
-- insert into Passengers (passenger_id, arrival_time) values ('13', '5')
-- insert into Passengers (passenger_id, arrival_time) values ('14', '6')
-- insert into Passengers (passenger_id, arrival_time) values ('15', '7')
# Write your MySQL query statement below
WITH RECURSIVE bus_lag_info AS (
SELECT
*,
LAG(arrival_time, 1, 0) OVER(ORDER BY arrival_time) AS prev_arrival_time -- 先通过LAG()开窗获得上一班车的到达时间
FROM
Buses
),
passenger_arrival_between_buses_info AS ( -- 统计出两辆bus间的到达的乘客
SELECT
b.bus_id,
b.arrival_time,
b.capacity,
COUNT(p.passenger_id) AS passenger_arrival_between_buses_cnt,
ROW_NUMBER() OVER(ORDER BY b.arrival_time) AS rn
FROM
bus_lag_info AS b
LEFT JOIN
Passengers AS p
ON
p.arrival_time > b.prev_arrival_time AND p.arrival_time <= b.arrival_time
GROUP BY
1, 2, 3
),
boarded_info AS (
( -- 第一辆bus到达
SELECT
bus_id,
LEAST(capacity, passenger_arrival_between_buses_cnt) AS boarded_cnt,
GREATEST(0, passenger_arrival_between_buses_cnt - capacity) AS left_cnt,
rn
FROM
passenger_arrival_between_buses_info
WHERE
rn = 1
)
UNION ALL
(
SELECT
p.bus_id,
LEAST(capacity, left_cnt + passenger_arrival_between_buses_cnt) AS boarded_cnt,
GREATEST(0, left_cnt + passenger_arrival_between_buses_cnt - capacity) AS left_cnt,
p.rn
FROM
passenger_arrival_between_buses_info p, boarded_info b
WHERE
p.rn = b.rn + 1
)
)
SELECT
bus_id,
boarded_cnt AS passengers_cnt
FROM
boarded_info
ORDER BY
bus_id
-- use variable
WITH t AS (
SELECT
bus_id,
capacity,
b.arrival_time AS time,
count(passenger_id) AS total_passenger
FROM
Buses AS b
LEFT JOIN
Passengers AS p
ON
b.arrival_time >= p.arrival_time
GROUP BY
bus_id
)
SELECT
bus_id,
passengers_cnt
FROM
(
SELECT
bus_id,
LEAST(total_passenger - @passenger_before, capacity) AS passengers_cnt,
capacity,
time,
@passenger_before:= LEAST(total_passenger, @passenger_before+capacity) AS p
FROM
(select @passenger_before := 0) as init,
t
) AS a
ORDER BY
bus_id