Consecutive Available Seats LeetCode Solution

This Leetcode problem Consecutive Available Seats LeetCode Solution is done in SQL.

List of all LeetCode Solution

Consecutive Available Seats LeetCode Solution

Consecutive Available Seats LeetCode Solution

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_idfree
11
20
31
41
51

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)
Scroll to Top