This Leetcode problem Consecutive Available Seats LeetCode Solution is done in SQL.
List of all LeetCode Solution
![Consecutive Available Seats LeetCode Solution](https://i0.wp.com/totheinnovation.com/wp-content/uploads/2024/02/LeetCode-Problem-Solution.png?resize=200%2C200&ssl=1)
Consecutive Available Seats LeetCode Solution
Table of Contents
Problem Statement
Several friends at a cinema ticket office would like to reserve consecutive available seats.
Write a query for all the consecutive available seats ordered by the seat_id using the following cinema table.
The result format is in the following example.
Example 1:
Input:
seat_id |
3 |
4 |
5 |
Output:
seat_id | free |
1 | 1 |
2 | 0 |
3 | 1 |
4 | 1 |
5 | 1 |
Explanation:
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.
Consecutive Available Seats LeetCode Solution MySQL
select
distinct c1.seat_id
from
cinema as c1
join cinema as c2
join cinema as c3 on c1.seat_id = c2.seat_id + 1 || c1.seat_id = c3.seat_id - 1
where
c1.free = 1
and c2.free = 1
and c3.free = 1;
Code language: SQL (Structured Query Language) (sql)