Consecutive Available Seats

• it doesn't work for the situation with seat_id={1,2,3,4,5,6}，free={1,1,0,1,1,1},

• @ppp123 Could you please explain why so? The code runs properly for the following input.

``````{"headers":{"cinema":["seat_id","free"]},"rows":{"cinema":[[1,1],[2,1],[3,0],[4,1],[5,1],[6,1]]}}
``````

And, the output is as below.

``````{"headers": ["seat_id"], "values": [[1], [2], [4], [5], [6]]}
``````

• @Mr.Bin This might just be an english issue but the question defines consecutive as "more than 2" (i.e >2, not >=2 ) free seats.

The solution selects 2 or more free seats. How would you adapt the solution to select strictly more than 2 free seats?

• @YashArya I'd better to modify the description to make it more clear. Regarding your question, you can check this problem instead https://leetcode.com/problems/consecutive-numbers/#/description, and I think you can work it out by yourself.

• this sql is not joining consicutive seats but 1with 3, 2 with 4 and so on.
this actual logic should be "a.seat_id - b.seat_id=1 or a.seat_id - b.seat_id=0" for previous and currenct seat.
then union with "a.seat_id - b.seat_id=-1 or a.seat_id - b.seat_id=0" for current and next seat.

• I got something is similar here:

SELECT DISTINCT c1.seat_id AS seat_id
FROM cinema c1, cinema c2
WHERE c1.free = 1
AND c2.free = 1
AND (c1.seat_id - c2.seat_id = 1
OR c2.seat_id - c1.seat_id = 1)
ORDER BY seat_id;

• ``````SELECT
distinct c1.seat_id
FROM
cinema c1, cinema c2
WHERE
c1.seat_id != c2.seat_id
AND
c1.free = 1
AND
c2.free = 1
AND
((c1.seat_id = c2.seat_id - 1) OR (c2.seat_id = c1.seat_id - 1))
ORDER BY
c1.seat_id
``````

• select distinct c1.seat_id
from cinema c1
join cinema c2
on c1.seat_id != c2.seat_id
where c1.free=1 and c2.free=1 and (c1.seat_id = c2.seat_id-1 or c1.seat_id = c2.seat_id+1)

• select distinct c1.seat_id
from cinema c1, cinema c2
where c1.free=1 and c2.free=1 and (c1.seat_id = c2.seat_id-1 or c1.seat_id = c2.seat_id+1)

• ``````select distinct c1.seat_id
from cinema c1
join cinema c2 on c1.seat_id != c2.seat_id
where c1.free = 1 and c2.free = 1 and abs(c1.seat_id - c2.seat_id) = 1
order by seat_id
``````

• select distinct c1.seat_id as seat_id
from cinema c1, cinema c2
where c1.free = 1
and c2.free = 1
and abs(c1.seat_id - c2.seat_id) = 1
order by 1

• select distinct s1.seat_id from
seat s1, seat s2
where s1.seat_id!=s2.seat_id and abs(s1.free-s2.free)=0
and s1.free=True and s2.free=True and abs(s1.seat_id-s2.seat_id)=1
order by s1.seat_id

• I have different logic, and my solution was accepted as well:
SELECT distinct seat_id
FROM cinema
WHERE free = 1 AND
(seat_id - 1 in (select seat_id FROM cinema WHERE free = 1) OR
seat_id + 1 in (select seat_id FROM cinema WHERE free = 1))
ORDER BY seat_id;

• SELECT SEAT_ID
FROM CINEMA
WHERE FREE = 1
AND (

SEAT_ID +1 IN (SELECT SEAT_ID FROM CINEMA WHERE FREE = 1 )
OR SEAT_ID -1 IN (SELECT SEAT_ID FROM CINEMA WHERE FREE = 1 )

);

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.