Maximum Theater Group Seating Capacity
Math & Bit ManipulationAuto-graded · 20 test casesLast updated
Tests bitmask-based interval availability checking for constrained seat allocation with large input optimization.
A movie theater contains n rows. Each row has exactly 10 seats numbered 1 through 10. Some seats are already reserved, provided as a list of [row, seat] pairs.
You need to determine the maximum number of 4-person groups that can still be seated. Each group must occupy 4 consecutive seats within a single row, and only the following three seat ranges are valid:
- Seats 2, 3, 4, 5
- Seats 4, 5, 6, 7
- Seats 6, 7, 8, 9
Return the maximum total number of groups that can be accommodated across all rows given the current reservations.
Constraints:
1 ≤ n ≤ 10^90 ≤ len(reserved_seats) ≤ 10^4- Each
[row, seat]pair satisfies1 ≤ row ≤ nand1 ≤ seat ≤ 10 - No seat is reserved more than once
- Reserved seats may appear in any order
Examples
Example 1
Input: max_groups_seated(3, [])
Output: 6
Example 2
Input: max_groups_seated(2, [[1, 2], [1, 3], [1, 8], [2, 6]])
Output: 2
Hints
Hint 1
Notice that seats 1 and 10 cannot be part of any valid 4-seat block. Only seats 2 through 9 matter.
Hint 2
Each row can accommodate at most 2 groups. This happens only when both blocks 2–5 and 6–9 are completely available.
Hint 3
Use a bitmask to track which seats in the critical range (2–9) are occupied in each row, enabling constant-time block availability checks.
Hint 4
Since n can be up to 10^9, you cannot iterate over all rows. Count unreserved rows separately and add their contribution in one step.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The key observation is that each row can hold at most 2 four-person groups, and this maximum is achievable only when specific seat blocks are available.
Critical insights:
- Seats 1 and 10 never participate in any valid block, so reservations there don't affect capacity.
- The three valid blocks are: LEFT (2–5), MIDDLE (4–7), and RIGHT (6–9).
- Only LEFT and RIGHT are non-overlapping. A row holds 2 groups only when both are completely free.
- If the {LEFT, RIGHT} pair is unavailable, at most 1 group fits, which happens when any single block is free.
- Since
ncan be extremely large (up to 10^9), we cannot iterate over all rows. Instead, we process only rows with reservations and count the rest in bulk.
Algorithm:
- Create bitmasks representing the three valid seat blocks.
- Build a dictionary mapping each row (that has reservations in seats 2–9) to a bitmask of its occupied seats.
- Initialize the count as
2 × (n - rows_with_reservations)— every unreserved row contributes 2 groups. - For each row with reservations:
- If both LEFT and RIGHT blocks are unoccupied → add 2 groups
- Else if any of LEFT, MIDDLE, or RIGHT is unoccupied → add 1 group
- Otherwise → add 0 groups
Why it works: Two groups require two disjoint 4-seat blocks. The only disjoint pair from our three options is {LEFT, RIGHT}. When that configuration is blocked, we check if any single block remains available. Since rows are independent, we sum the per-row capacities.
Complexity:
- Time: O(R) where R = number of reserved seats
- Space: O(distinct rows with reservations in seats 2–9)
Both are independent of n, making the solution efficient even when n = 10^9.
Complete Solution
from typing import List
def max_groups_seated(n: int, reserved_seats: List[List[int]]) -> int:
# Precompute bitmasks for the three valid blocks (using 0-indexed bit positions)
LEFT = sum(1 << (s - 1) for s in range(2, 6)) # seats 2-5
MIDDLE = sum(1 << (s - 1) for s in range(4, 8)) # seats 4-7
RIGHT = sum(1 << (s - 1) for s in range(6, 10)) # seats 6-9
# Track occupancy bitmask for rows that have reservations in seats 2-9
row_masks = {}
for row, seat in reserved_seats:
if 2 <= seat <= 9: # seats 1 and 10 are irrelevant
row_masks[row] = row_masks.get(row, 0) | (1 << (seat - 1))
# Start with maximum possible: 2 groups per unreserved row
total = 2 * (n - len(row_masks))
# For each row with reservations, calculate how many groups fit
for mask in row_masks.values():
left_free = (mask & LEFT) == 0
right_free = (mask & RIGHT) == 0
if left_free and right_free:
total += 2 # both non-overlapping blocks available
elif left_free or right_free or (mask & MIDDLE) == 0:
total += 1 # at least one block is available
# else: all blocks blocked, add 0
return total