-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3140-ConsecutiveAvailableSeatsII.sql
More file actions
154 lines (145 loc) · 4.2 KB
/
3140-ConsecutiveAvailableSeatsII.sql
File metadata and controls
154 lines (145 loc) · 4.2 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
-- 3140. Consecutive Available Seats II
-- Table: Cinema
-- +-------------+------+
-- | Column Name | Type |
-- +-------------+------+
-- | seat_id | int |
-- | free | bool |
-- +-------------+------+
-- seat_id is an auto-increment column for this table.
-- Each row of this table indicates whether the ith seat is free or not. 1 means free while 0 means occupied.
-- Write a solution to find the length of longest consecutive sequence of available seats in the cinema.
-- Note:
-- There will always be at most one longest consecutive sequence.
-- If there are multiple consecutive sequences with the same length, include all of them in the output.
-- Return the result table ordered by first_seat_id in ascending order.
-- The result format is in the following example.
-- Example:
-- Input:
-- Cinema table:
-- +---------+------+
-- | seat_id | free |
-- +---------+------+
-- | 1 | 1 |
-- | 2 | 0 |
-- | 3 | 1 |
-- | 4 | 1 |
-- | 5 | 1 |
-- +---------+------+
-- Output:
-- +-----------------+----------------+-----------------------+
-- | first_seat_id | last_seat_id | consecutive_seats_len |
-- +-----------------+----------------+-----------------------+
-- | 3 | 5 | 3 |
-- +-----------------+----------------+-----------------------+
-- Explanation:
-- Longest consecutive sequence of available seats starts from seat 3 and ends at seat 5 with a length of 3.
-- Output table is ordered by first_seat_id in ascending order.
-- CREATE TABLE if Not exists Cinema (
-- seat_id INT PRIMARY KEY AUTO_INCREMENT,
-- free BOOLEAN
-- )
-- Truncate table Cinema
-- insert into Cinema (seat_id, free) values ('1', '1')
-- insert into Cinema (seat_id, free) values ('2', '0')
-- insert into Cinema (seat_id, free) values ('3', '1')
-- insert into Cinema (seat_id, free) values ('4', '1')
-- insert into Cinema (seat_id, free) values ('5', '1')
-- # Write your MySQL query statement below
-- WITH t AS (
-- SELECT
-- *,
-- seat_id - (RANK() OVER(ORDER BY seat_id )) AS diff
-- FROM
-- (SELECT * FROM Cinema WHERE free = 1) AS c
-- )
-- -- SELECT * FROM t
-- -- | seat_id | free | diff |
-- -- | ------- | ---- | ---- |
-- -- | 1 | 1 | 0 |
-- -- | 3 | 1 | 1 |
-- -- | 4 | 1 | 1 |
-- -- | 5 | 1 | 1 |
-- SELECT
-- MIN(seat_id) AS first_seat_id,
-- MAX(seat_id) AS last_seat_id,
-- COUNT(*) AS consecutive_seats_len
-- FROM
-- t
-- WHERE
-- diff = (
-- SELECT
-- diff
-- FROM
-- t
-- GROUP BY
-- diff
-- ORDER BY
-- COUNT(*) DESC
-- LIMIT 1
-- )
WITH t AS (
SELECT
*,
seat_id - (RANK() OVER(ORDER BY seat_id )) AS diff
FROM
(SELECT * FROM Cinema WHERE free = 1) AS c
),
s AS (
SELECT
diff,
COUNT(*) AS cnt
FROM
t
GROUP BY
diff
),
c AS (
SELECT
*,
RANK() OVER(PARTITION BY diff ORDER BY seat_id) AS rk
FROM
t
WHERE
diff IN (
SELECT diff FROM s WHERE cnt = (SELECT MAX(cnt) FROM s)
)
)
-- SELECT * FROM s
-- SELECT
-- MIN(seat_id) AS first_seat_id,
-- MAX(seat_id) AS last_seat_id,
-- COUNT(*) AS consecutive_seats_len
-- FROM
-- t
-- WHERE
-- diff IN (
-- SELECT diff FROM s WHERE cnt = (SELECT MAX(cnt) FROM s)
-- )
-- SELECT
-- *,
-- RANK() OVER(PARTITION BY diff ORDER BY seat_id) AS rk
-- FROM
-- t
-- WHERE
-- diff IN (
-- SELECT diff FROM s WHERE cnt = (SELECT MAX(cnt) FROM s)
-- )
-- | seat_id | free | diff | rk |
-- | ------- | ---- | ---- | -- |
-- | 21 | 1 | 10 | 1 |
-- | 22 | 1 | 10 | 2 |
-- | 23 | 1 | 10 | 3 |
-- | 24 | 1 | 10 | 4 |
-- | 31 | 1 | 14 | 1 |
-- | 32 | 1 | 14 | 2 |
-- | 33 | 1 | 14 | 3 |
-- | 34 | 1 | 14 | 4 |
SELECT
MIN(seat_id) AS first_seat_id,
MAX(seat_id) AS last_seat_id,
COUNT(*) AS consecutive_seats_len
FROM
c
GROUP BY
diff