Consecutive Available Seats II - Problem

šŸŽ¬ Cinema Seat Optimization

Imagine you're building a movie theater booking system! You have a cinema with seats arranged in a single row, where each seat can either be available (free = 1) or occupied (free = 0).

Your task is to find the longest consecutive sequence of available seats. This is crucial for accommodating large groups who want to sit together!

Goal: Find all sequences with the maximum length of consecutive available seats.

Input: A table Cinema with columns:

  • seat_id (int): Sequential seat number
  • free (bool): 1 = available, 0 = occupied

Output: For each longest sequence, return:

  • first_seat_id: Starting seat of the sequence
  • last_seat_id: Ending seat of the sequence
  • consecutive_seats_len: Length of the sequence

Example: Seats [1,2,3,4,5] with availability [1,0,1,1,1] → longest sequence is seats 3-5 (length 3)

Input & Output

example_1.sql — Basic Case
$ Input: Cinema table: +---------+------+ | seat_id | free | +---------+------+ | 1 | 1 | | 2 | 0 | | 3 | 1 | | 4 | 1 | | 5 | 1 | +---------+------+
› Output: +-----------------+----------------+-----------------------+ | first_seat_id | last_seat_id | consecutive_seats_len | +-----------------+----------------+-----------------------+ | 3 | 5 | 3 | +-----------------+----------------+-----------------------+
šŸ’” Note: Seats 1 is available alone (length 1), while seats 3,4,5 form a consecutive sequence (length 3). The maximum length is 3, so we return the sequence starting at seat 3 and ending at seat 5.
example_2.sql — Multiple Equal Sequences
$ Input: Cinema table: +---------+------+ | seat_id | free | +---------+------+ | 1 | 1 | | 2 | 1 | | 3 | 0 | | 4 | 1 | | 5 | 1 | | 6 | 0 | | 7 | 1 | +---------+------+
› Output: +-----------------+----------------+-----------------------+ | first_seat_id | last_seat_id | consecutive_seats_len | +-----------------+----------------+-----------------------+ | 1 | 2 | 2 | | 4 | 5 | 2 | +-----------------+----------------+-----------------------+
šŸ’” Note: There are three available sequences: seats 1-2 (length 2), seats 4-5 (length 2), and seat 7 (length 1). The maximum length is 2, so we return both sequences with length 2, ordered by first_seat_id.
example_3.sql — All Seats Available
$ Input: Cinema table: +---------+------+ | seat_id | free | +---------+------+ | 1 | 1 | | 2 | 1 | | 3 | 1 | | 4 | 1 | +---------+------+
› Output: +-----------------+----------------+-----------------------+ | first_seat_id | last_seat_id | consecutive_seats_len | +-----------------+----------------+-----------------------+ | 1 | 4 | 4 | +-----------------+----------------+-----------------------+
šŸ’” Note: All seats are available and consecutive, forming one sequence from seat 1 to seat 4 with length 4. This is the only sequence and also the maximum length.

Time & Space Complexity

Time Complexity
ā±ļø
O(n log n)

Single scan with ORDER BY for ROW_NUMBER(), where n is the number of seats

n
2n
⚔ Linearithmic
Space Complexity
O(n)

Temporary storage for grouping and window function results

n
2n
⚔ Linearithmic Space

Constraints

  • 1 ≤ seat_id ≤ 105
  • free is either 0 (occupied) or 1 (available)
  • seat_id values are unique and auto-incrementing
  • There will always be at most one longest consecutive sequence length (but multiple sequences can have this same maximum length)
Asked in
25.0K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
šŸ’” Explanation
AI Ready
šŸ’” Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen