Consecutive Available Seats LeetCode Solution

Last updated on October 9th, 2024 at 10:16 pm

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

List of all LeetCode Solution

Level of Question

Easy

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.

1. 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;
Scroll to Top