Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion Easy/Consecutive available seats.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@
-- The seat_id is an auto increment int, and free is bool ('1' means free, and '0' means occupied.).
-- Consecutive available seats are more than 2(inclusive) seats consecutively available.

--My Solution
//METHOD 1
SELECT DISTINCT c1.seat_id
FROM cinema c1
JOIN cinema c2 ON ABS(c1.seat_id - c2.seat_id) = 1
Where c1.free = 1 AND c2.free = 1

//METHOD 2
SELECT c1.seat_id
FROM cinema c1
JOIN cinema c2 on c1.seat_id = c2.seat_id - 1
where c1.free = 1 AND c2.free = 1

UNION

SELECT c2.seat_id
FROM cinema c1
JOIN cinema c2 on c1.seat_id = c2.seat_id + 1
where c1.free = 1 AND c2.free = 1

-- Solution
Select seat_id
from(
Expand All @@ -30,4 +50,4 @@ lead(free,1) over() as next,
lag(free,1) over() as prev
from cinema) a
where a.free=True and (next = True or prev=True)
order by seat_id
order by seat_id