Cinema Seat Allocation LeetCode solution in C++

1
class Solution {
2
public:
3
int maxNumberOfFamilies(int n, vector<vector<int>> &reservedSeats) {
4
unordered_map<int, vector<int>> o;
5

6
for (auto r : reservedSeats) {
7
o[r[0]].push_back(r[1]);
8
}
9

10
int ans = 2 * (n - o.size()); // seats with no occupancy directly adding 2
11

12
// if we iterate from 1 to n (10 ^ 9) we cross time limit
13
for (auto p : o) {
14
// get reserved seats
15
vector<int> seats = p.second;
16

17
int occupied_mask = 0;
18

19
// representing occupied seats as bits
20
for (int s : seats) {
21
if (s > 1 && s < 10) {
22
occupied_mask += (int)pow(2, 10 - s);
23
}
24
}
25

26
// checking 3 seating configurations
27
int masks[3] = {480, 120, 30};
28

29
int row_ans = 0;
30
bool res1 = (masks[0] & occupied_mask) == 0;
31
bool res2 = (masks[1] & occupied_mask) == 0;
32
bool res3 = (masks[2] & occupied_mask) == 0;
33

34
// if all 3 configurations are empty, 2 families
35
// if either one of them are empty, 1 family
36
if (res1 && res2 && res3)
37
row_ans += 2;
38
else if (res1 || res2 || res3)
39
row_ans += 1;
40

41
ans += row_ans;
42
}
43

44
return ans;
45
}
46
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0