-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path603-ConsecutiveAvailableSeats.sql
More file actions
69 lines (65 loc) · 1.85 KB
/
603-ConsecutiveAvailableSeats.sql
File metadata and controls
69 lines (65 loc) · 1.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
-- 603. Consecutive Available Seats
-- Table: Cinema
-- +-------------+------+
-- | Column Name | Type |
-- +-------------+------+
-- | seat_id | int |
-- | free | bool |
-- +-------------+------+
-- seat_id is an auto-increment primary key 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 an SQL query to report all the consecutive available seats in the cinema.
-- Return the result table ordered by seat_id in ascending order.
-- The test cases are generated so that more than two seats are consecutively available.
-- The query result format is in the following example.
--
-- Example 1:
-- Input:
-- Cinema table:
-- +---------+------+
-- | seat_id | free |
-- +---------+------+
-- | 1 | 1 |
-- | 2 | 0 |
-- | 3 | 1 |
-- | 4 | 1 |
-- | 5 | 1 |
-- +---------+------+
-- Output:
-- +---------+
-- | seat_id |
-- +---------+
-- | 3 |
-- | 4 |
-- | 5 |
-- +---------+
-- Create table If Not Exists Cinema (seat_id int primary key auto_increment, free bool)
-- 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
SELECT
DISTINCT a.seat_id AS seat_id
FROM
Cinema AS a,
Cinema AS b
WHERE
(a.seat_id = b.seat_id + 1 OR a.seat_id = b.seat_id - 1) AND
a.free = 1 AND b.free = 1
ORDER BY
a.seat_id
-- best solution
SELECT
DISTINCT c1.seat_id AS seat_id
FROM
cinema AS c1,cinema AS c2
WHERE
c1.free =1 AND
c2.free=1 AND
ABS(c1.seat_id - c2.seat_id) = 1
ORDER BY
c1.seat_id;