Cinema Seat Allocation - Problem

Welcome to the Cinema Seat Allocation problem! Imagine you're working for a popular movie theater chain, and your job is to maximize the number of four-person groups that can be seated together.

Each cinema has n rows of seats, numbered from 1 to n. Every row contains exactly 10 seats labeled 1 through 10, with a layout like this:

[1][2][3] | [4][5][6][7] | [8][9][10]

Notice the aisles (|) that separate the seats into three sections. A four-person group needs four adjacent seats in the same row. The tricky part? There are three possible arrangements:

  • Left section: seats [2,3,4,5] (crosses the left aisle)
  • Middle section: seats [4,5,6,7] (entirely in the middle)
  • Right section: seats [6,7,8,9] (crosses the right aisle)

Given an array reservedSeats where reservedSeats[i] = [row, seat] represents already booked seats, your task is to find the maximum number of four-person groups you can accommodate.

Goal: Return the maximum number of complete 4-person groups that can be seated.

Input & Output

example_1.py โ€” Basic Case
$ Input: n = 3, reservedSeats = [[1,2],[1,3],[1,8],[2,6],[3,1],[3,10]]
โ€บ Output: 4
๐Ÿ’ก Note: Row 1: Seats 2,3 blocked โ†’ Left pattern blocked, but middle [4,5,6,7] and right [6,7,8,9] available โ†’ Choose left+right impossible, so pick middle โ†’ 1 group. Row 2: Seat 6 blocked โ†’ Left [2,3,4,5] available, middle blocked, right available โ†’ Choose left+right โ†’ 2 groups. Row 3: Seats 1,10 blocked โ†’ All patterns available โ†’ 2 groups. Total: 1+2+1=4
example_2.py โ€” All Available
$ Input: n = 2, reservedSeats = []
โ€บ Output: 4
๐Ÿ’ก Note: No reserved seats, so each row can accommodate 2 groups (left+right patterns [2,3,4,5] and [6,7,8,9]). Total: 2*2 = 4 groups.
example_3.py โ€” Heavy Blocking
$ Input: n = 4, reservedSeats = [[4,3],[1,4],[4,6],[1,7]]
โ€บ Output: 4
๐Ÿ’ก Note: Row 1: Seats 4,7 blocked โ†’ All three patterns blocked โ†’ 0 groups. Row 2,3: No reservations โ†’ 2 groups each. Row 4: Seats 3,6 blocked โ†’ All patterns blocked โ†’ 0 groups. Total: 0+2+2+0=4

Visualization

Tap to expand
๐ŸŽญ Cinema Seat Allocation StrategyMovie Theater Layout12345678910โœ“ Left Group: [2,3,4,5]โœ“ Middle Group: [4,5,6,7]โœ“ Right Group: [6,7,8,9]๐ŸŽฏ Key Strategy: Maximize non-overlapping patterns per rowBest case: Left + Right = 2 groups | Alternative: Middle = 1 group
Understanding the Visualization
1
Map the Layout
Visualize cinema rows with aisles: [1][2][3]|[4][5][6][7]|[8][9][10]
2
Identify Patterns
Three valid 4-seat groups: Left [2,3,4,5], Middle [4,5,6,7], Right [6,7,8,9]
3
Check Conflicts
Use reserved seats to eliminate blocked patterns
4
Maximize Groups
Choose left+right when possible (2 groups), otherwise pick any available pattern (1 group)
Key Takeaway
๐ŸŽฏ Key Insight: Each row supports at most 2 four-person groups. Use bit manipulation to efficiently check pattern availability and prioritize non-overlapping arrangements (left+right) over middle pattern when possible.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(r)

Where r is the number of reserved seats. We only process rows that have reservations, not all n rows

n
2n
โœ“ Linear Growth
Space Complexity
O(r)

Hash map stores reserved seats grouped by row, maximum r entries total

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค n โ‰ค 109
  • 1 โ‰ค reservedSeats.length โ‰ค 4 * 104
  • reservedSeats[i].length == 2
  • 1 โ‰ค reservedSeats[i][0] โ‰ค n
  • 1 โ‰ค reservedSeats[i][1] โ‰ค 10
  • Each seat appears at most once in reservedSeats
Asked in
Google 45 Amazon 38 Microsoft 29 Meta 22
38.2K Views
Medium Frequency
~18 min Avg. Time
1.5K 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