-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcinemaSeat.txt
44 lines (37 loc) · 1.59 KB
/
cinemaSeat.txt
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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.
class Solution {
public:
int maxNumberOfFamilies(int n, vector<vector<int>>& rs) {
int i = 0,count = 0,prev = 0;
sort(rs.begin(),rs.end());
vector<int> v;
count+=2*(n-rs[rs.size()-1][0]);
while(i<rs.size())
{
v = vector<int>(10,1);
int rownum = rs[i][0];
if((prev+1)!=rownum) count+=2*(rownum-prev-1);
prev = rownum;
while(i<rs.size() && rs[i][0]==rownum)
{
v[rs[i][1]-1] = 0;
i++;
}
int c1 = v[1] & v[2] & v[3] & v[4] & v[5] & v[6] & v[7] & v[8];
int c2 = (v[1] && v[2] && v[3] && v[4]) | (v[3] & v[4] & v[5] & v[6]) | (v[5] & v[6] & v[7] & v[8]);
if(c1) count+=2;
else if(c2) count+=1;
}
return count;
}
};