Cinema Seat Allocation (LeetCode 1386): Solution and Explanation

Medium
Array
Hash Table
Greedy
Bit Manipulation

A cinema has n rows of ten seats with aisles after seats 3 and 7. Given the reserved seats, place as many groups of four adjacent seats as possible, where a group may be split two and two by the middle aisle. With n = 3 and reservedSeats = [[1,2],[1,3],[1,8],[2,6],[3,1],[3,10]] the answer is 4.

Intuition

The aisles leave only three places a group of four can sit: seats 2-5, seats 4-7 (split by the aisle, two on each side) and seats 6-9. Seats 1 and 10 can never be part of a group.

A row can hold at most two groups, and only as 2-5 plus 6-9, because the middle block overlaps both. So each row is worth 2, 1 or 0.

n can be a billion, but there are at most 10000 reservations. Every row that never appears is worth exactly 2, so those rows are counted in one multiplication.

For a reserved row, put its taken seats 2 to 9 into an 8-bit mask. A block is free exactly when the mask AND the block's mask is 0.

Approach: Row bitmasks

  1. For each reservation with a seat from 2 to 9, set bit seat - 2 in that row's mask.
  2. Start total = 2 * (n - number of masked rows).
  3. For each masked row: if both left (0b00001111) and right (0b11110000) are free, add 2.
  4. Otherwise, if any of left, middle (0b00111100) or right is free, add 1.
  5. Return total.

Step-by-step example

n = 3, reservedSeats = [[1,2],[1,3],[1,8],[2,6],[3,1],[3,10]]
  1. Row 1 gets seats 2, 3 and 8: mask 01000011. Row 2 gets seat 6: mask 00010000. Seats 1 and 10 in row 3 are ignored.
  2. Two rows have a mask, so total starts at 2 * (3 - 2) = 2 for row 3.
  3. Row 1: left and right are both blocked, middle 4-7 is free, so total = 3.
  4. Row 2: left 2-5 is free, right is blocked, so total = 4.
  5. Return 4.

Code: Python, Java, C++

1class Solution:
2    def maxNumberOfFamilies(self, n: int, reservedSeats: List[List[int]]) -> int:
3        taken = {}
4        for row, seat in reservedSeats:
5            if 2 <= seat <= 9:
6                taken[row] = taken.get(row, 0) | (1 << (seat - 2))
7        left, middle, right = 0b00001111, 0b00111100, 0b11110000
8        total = 2 * (n - len(taken))
9        for mask in taken.values():
10            if mask & left == 0 and mask & right == 0:
11                total += 2
12            elif mask & left == 0 or mask & middle == 0 or mask & right == 0:
13                total += 1
14        return total
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(m)

Each of the m reservations is read once and each reserved row is checked with three AND operations.

Space

O(m)

The map holds at most one mask per reserved row.

Other approaches

Sets of seats per row (time O(m), space O(m))

Store each reserved row's seats in a set and check the three blocks seat by seat. Same complexity with a larger constant.

Scan every row (time O(n + m), space O(n))

Check all n rows directly. Correct, but n can be a billion.

Common mistakes

  • Looping over all n rows. With n up to 10^9 that times out; untouched rows must be counted with a multiplication.
  • Counting a row whose only reservations are seats 1 or 10 as reserved. Those seats block nothing, so the row is still worth 2.
  • Adding the middle block on top of left or right. The middle overlaps both, so a row never holds more than two groups.
  • Treating seats 3-6 or 5-8 as valid. Those would put three people on one side of an aisle and one on the other.

Frequently asked questions

Why check left and right before middle?

Left plus right is the only way to fit two groups. If either is blocked, at most one group fits, and any one free block gives it.

Can 2 * n overflow?

With n at most 10^9 the result is at most 2 * 10^9, which still fits in a signed 32-bit integer.