华为校招主管面

原题链接:
https://leetcode.cn/problems/cinema-seat-allocation/description/

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
class Solution {
private int left = 0b11110000;
private int middle = 0b11000011;
private int right = 0b00001111;

public int maxNumberOfFamilies(int n, int[][] reservedSeats) {
HashMap<Integer,Integer>hash = new HashMap<>();

for(int i = 0; i < reservedSeats.length ; i++){
int row = reservedSeats[i][0];
int col = reservedSeats[i][1];
// 最左和最右两个位置为 无效位置
if(col <= 1 || col >= 10){
continue;
}
int mask = hash.getOrDefault(row,0);
// 左右两个位置 无效直接忽略
hash.put(row, mask | 1 << (col - 2));
}

// 直接反向做可以加速 否则会超时
int ans = (n - hash.size()) * 2;

// 只对 有预约的行进行操作
for(var es : hash.entrySet()){
int row = es.getKey();
int seats = es.getValue();

if((seats | left) == left || (seats | middle) == middle || (seats | right) == right){
ans++;
}
}

return ans;
}
}