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,8 blocked → Left pattern [2,3,4,5] blocked, middle [4,5,6,7] available, right [6,7,8,9] blocked → 1 group. Row 2: Seat 6 blocked → Left [2,3,4,5] available, middle and right blocked → 1 group. Row 3: Seats 1,10 blocked → All patterns available → 2 groups. Total: 1+1+2=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

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

Visualization

Tap to expand
Cinema Seat Allocation INPUT n = 3 rows, 10 seats each Row 1: 1 2 3 4 5 6 7 8 9 10 Row 2: 1 2 3 4 5 6 7 8 9 10 Row 3: 1 2 3 4 5 6 7 8 9 10 reservedSeats = [[1,2],[1,3],[1,8], [2,6],[3,1],[3,10]] Available Reserved Valid 4-seat groups: [2,3,4,5] [4,5,6,7] [6,7,8,9] Left | Middle | Right ALGORITHM STEPS 1 Build Hash Map Group reserved seats by row Row 1: {2, 3, 8} Row 2: {6} Row 3: {1, 10} 2 Check Each Row Try all 3 valid positions 3 Count Valid Groups Max 2 groups per row Row 1: Middle OK (4-7) = 1 Row 2: Left + Right OK = 2 Row 3: Left + Right OK = 2 (But Left/Right overlap!) 4 Handle Overlaps Left+Right blocks Middle Empty rows get 2 groups each FINAL RESULT Optimal Group Placement Row 1: 1 group Row 2: 2 groups Row 3: 1 group Group 1 Group 2 Reserved Total Calculation Row 1: 1 group Row 2: 2 groups Row 3: 1 group OUTPUT 4 Key Insight: Use a HashMap to group reservations by row. For each row with reservations, check which of the 3 valid positions [2-5], [4-7], [6-9] are available. Note: if both Left and Right are free but Middle is blocked, you can fit 2 groups. Empty rows (not in HashMap) can always fit 2 groups. TutorialsPoint - Cinema Seat Allocation | Hash Map Approach
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