From 3e36d975900f84e000bd42e4b7d3adfa02072c78 Mon Sep 17 00:00:00 2001 From: anisha-singhal Date: Sat, 17 May 2025 00:55:52 +0530 Subject: [PATCH] Update Consecutive available seats.sql --- Easy/Consecutive available seats.sql | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/Easy/Consecutive available seats.sql b/Easy/Consecutive available seats.sql index e11b885..7ea115d 100644 --- a/Easy/Consecutive available seats.sql +++ b/Easy/Consecutive available seats.sql @@ -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( @@ -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 \ No newline at end of file +order by seat_id