[LeetCode] Cinema Seat Allocation

1386. Cinema Seat Allocation

A cinema has n rows of seats, numbered from 1 to n and there are ten seats in each row, labelled from 1 to 10 as shown in the figure above.

Given the array reservedSeats containing the numbers of seats already reserved, for example, reservedSeats[i] = [3,8] means the seat located in row 3 and labelled with 8 is already reserved.

Return the maximum number of four-person groups you can assign on the cinema seats. A four-person group occupies four adjacent seats in one single row. Seats across an aisle (such as [3,3] and [3,4]) are not considered to be adjacent, but there is an exceptional case on which an aisle split a four-person group, in that case, the aisle split a four-person group in the middle, which means to have two people on each side.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
public:
int maxNumberOfFamilies(int n, vector<vector<int>>& reservedSeats) {
int res = n << 1;
const int left = 1, right = (1<<2), leftmid = left | (1<<1), rightmid = right | (1<<1);
const int all = left | rightmid;
unordered_map<int, short> m;

for(auto& seat : reservedSeats) {
switch (seat.back()) {
case 2:
case 3: m[seat.front()] |= left; break;
case 4:
case 5: m[seat.front()] |= leftmid; break;
case 6:
case 7: m[seat.front()] |= rightmid; break;
case 8:
case 9: m[seat.front()] |= right; break;
}
}

for(auto& seat : m) {
res -= seat.second ^ all ? 1 : 2;
}

return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2021/04/13/PS/LeetCode/cinema-seat-allocation/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.