š¬ 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 numberfree(bool): 1 = available, 0 = occupied
Output: For each longest sequence, return:
first_seat_id: Starting seat of the sequencelast_seat_id: Ending seat of the sequenceconsecutive_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
Time & Space Complexity
Single scan with ORDER BY for ROW_NUMBER(), where n is the number of seats
Temporary storage for grouping and window function results
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)