Cinema Seat Allocation (LeetCode 1386): Solution and Explanation
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
- For each reservation with a seat from 2 to 9, set bit
seat - 2in that row's mask. - Start
total = 2 * (n - number of masked rows). - For each masked row: if both left (
0b00001111) and right (0b11110000) are free, add 2. - Otherwise, if any of left, middle (
0b00111100) or right is free, add 1. - Return
total.
Step-by-step example
n = 3, reservedSeats = [[1,2],[1,3],[1,8],[2,6],[3,1],[3,10]]
- 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.
- Two rows have a mask, so total starts at 2 * (3 - 2) = 2 for row 3.
- Row 1: left and right are both blocked, middle 4-7 is free, so total = 3.
- Row 2: left 2-5 is free, right is blocked, so total = 4.
- 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 total1class Solution {
2 public int maxNumberOfFamilies(int n, int[][] reservedSeats) {
3 Map<Integer, Integer> taken = new HashMap<>();
4 for (int[] r : reservedSeats) {
5 if (r[1] >= 2 && r[1] <= 9) {
6 taken.merge(r[0], 1 << (r[1] - 2), (a, b) -> a | b);
7 }
8 }
9 int left = 0b00001111, middle = 0b00111100, right = 0b11110000;
10 int total = 2 * (n - taken.size());
11 for (int mask : taken.values()) {
12 if ((mask & left) == 0 && (mask & right) == 0) {
13 total += 2;
14 } else if ((mask & left) == 0 || (mask & middle) == 0 || (mask & right) == 0) {
15 total += 1;
16 }
17 }
18 return total;
19 }
20}1class Solution {
2public:
3 int maxNumberOfFamilies(int n, vector<vector<int>>& reservedSeats) {
4 unordered_map<int, int> taken;
5 for (auto& r : reservedSeats) {
6 if (r[1] >= 2 && r[1] <= 9) {
7 taken[r[0]] |= 1 << (r[1] - 2);
8 }
9 }
10 int left = 0b00001111, middle = 0b00111100, right = 0b11110000;
11 int total = 2 * (n - (int)taken.size());
12 for (auto& kv : taken) {
13 if ((kv.second & left) == 0 && (kv.second & right) == 0) {
14 total += 2;
15 } else if ((kv.second & left) == 0 || (kv.second & middle) == 0 || (kv.second & right) == 0) {
16 total += 1;
17 }
18 }
19 return total;
20 }
21};Time and space complexity
O(m)
Each of the m reservations is read once and each reserved row is checked with three AND operations.
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.