Cinema Seat Allocation Problem
Description
LeetCode Problem 1386.

A cinema has n rows of seats, numbered from 1 to n. Each row has 10 seats, numbered from 1 to 10.
You are given a 2D integer array reservedSeats, where reservedSeats[i] = [row_i, seat_i] means that seat seat_i in row row_i is already reserved.
A four-person group must be assigned to four seats in the same row. The group can be seated in one of the following seat blocks:
- seats
2, 3, 4, 5 - seats
4, 5, 6, 7 - seats
6, 7, 8, 9
A block can be used only if none of its seats are reserved. Each seat can be assigned to at most one group.
Return an integer denoting the maximum number of four-person groups that can be assigned.
Example 1:

Input: n = 3, reservedSeats = [[1,2],[1,3],[1,8],[2,6],[3,1],[3,10]]
Output: 4
Explanation: The figure above shows an optimal allocation of four groups. Seats marked in blue are already reserved, and each set of four contiguous seats marked in orange is assigned to one group.
Example 2:
Input: n = 2, reservedSeats = [[2,1],[1,8],[2,6]]
Output: 2
Example 3:
Input: n = 4, reservedSeats = [[4,3],[1,4],[4,6],[1,7]]
Output: 4
Constraints:
1 <= n <= 10^91 <= reservedSeats.length <= min(10 * n, 10^4)reservedSeats[i] == [row_i, seat_i]1 <= row_i <= n1 <= seat_i <= 10- All
reservedSeats[i]are distinct.
Solution
Approach: Hash Map of Row Bitmasks
With n up to 109, we cannot look at every row. But at most 104 rows have a reservation, and every other row is empty and fits exactly two groups (seats 2-5 and 6-9). So the answer starts at 2 * (n - number of rows with reservations), and we only examine the reserved rows.
For each reserved row, store its reserved seats as a bitmask (bit s for seat s) in a hash map. Then, for each row's mask:
- If seats 2-5 and seats 6-9 are both free, the row fits 2 groups. This is the only way to fit two, since any two of the three blocks other than this pair overlap.
- Otherwise, if any one of the blocks 2-5, 4-7 or 6-9 is free, it fits 1 group.
- Otherwise it fits 0.
A block is free when (mask & block) == 0, with block masks for seats 2-5, 4-7 and 6-9. Seats 1 and 10 are never part of a block, so reserving them changes nothing.
The answer can be close to 2 * 109, which still fits in a 32-bit signed integer.
Complexity: O(r) time and O(r) extra space, where r is the length of reservedSeats.
class Solution:
def maxNumberOfFamilies(self, n: int, reservedSeats: List[List[int]]) -> int:
reserved = {} # row -> bitmask of its reserved seats (bit s for seat s)
for row, seat in reservedSeats:
reserved[row] = reserved.get(row, 0) | (1 << seat)
LEFT = 0b0000111100 # seats 2-5
MIDDLE = 0b0011110000 # seats 4-7
RIGHT = 0b1111000000 # seats 6-9
# A row with no reservation fits two groups (seats 2-5 and 6-9).
groups = 2 * (n - len(reserved))
for mask in reserved.values():
left_free = (mask & LEFT) == 0
right_free = (mask & RIGHT) == 0
if left_free and right_free:
groups += 2
elif left_free or right_free or (mask & MIDDLE) == 0:
groups += 1
return groups