# LEETCODE 1386. Cinema Seat Allocation 解题思路分析

```输入：n = 3, reservedSeats = [[1,2],[1,3],[1,8],[2,6],[3,1],[3,10]]

```输入：n = 2, reservedSeats = [[2,1],[1,8],[2,6]]

```输入：n = 4, reservedSeats = [[4,3],[1,4],[4,6],[1,7]]

• 1 <= n <= 10^9
• 1 <= reservedSeats.length <= min(10*n, 10^4)
• reservedSeats[i].length == 2
• 1 <= reservedSeats[i][0] <= n
• 1 <= reservedSeats[i][1] <= 10
• 所有 reservedSeats[i] 都是互不相同的。

1. 如果当前座位在第2列或者第3列，设置2-5列不能安排连坐。
2. 如果当前座位在第4列或者第5列，设置2-5列不能安排连坐，并且4-7列不能安排连坐
3. 如果当前座位在第6列或者第7列，设置4-7列不能安排连坐，并且6-9列不能安排连坐
4. 如果当前座位在第8列或者第9列，设置6-9列不能安排连坐。

```public int maxNumberOfFamilies(int n, int[][] reservedSeats) {
Arrays.sort(reservedSeats,(a,b)->a[0]-b[0]);
int res=(reservedSeats[0][0]-1)*2;
boolean canSitRight=true,canSitMiddle=true,canSitLeft=true;
for(int i=0;i<reservedSeats.length;i++){
int[] s=reservedSeats[i];
if(s[1]==2||s[1]==3){
canSitLeft=false;
}else if(s[1]==4||s[1]==5){
canSitLeft=false;
canSitMiddle=false;
}else if(s[1]==6||s[1]==7){
canSitMiddle=false;
canSitRight=false;
}else if(s[1]==8||s[1]==9){
canSitRight=false;
}
if(i<reservedSeats.length-1&&reservedSeats[i+1][0]!=s[0]
|| i==reservedSeats.length-1){
if(i==reservedSeats.length-1){
res+=(n-s[0])*2;
}else{
res+=(reservedSeats[i+1][0]-s[0]-1)*2;
}
if(canSitRight&&canSitLeft) res+=2;
else if(canSitRight||canSitMiddle||canSitLeft)res+=1;
canSitRight=true;
canSitMiddle=true;
canSitLeft=true;
}
}
return res;
}```

Runtime: 31 ms, faster than 37.69% of Java online submissions for Cinema Seat Allocation.

Memory Usage: 45.3 MB, less than 100.00% of Java online submissions for Cinema Seat Allocation.