import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author xnl
* @Description:
* @date: 2022/7/20 22:12
*/
public class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
}
public int maxNumberOfFamilies(int n, int[][] reservedSeats) {
int left = 0b11110000;
int middle = 0b11000011;
int right = 0b00001111;
Map<Integer, Integer> map = new HashMap<>(16);
for (int[] reservedSeat : reservedSeats) {
// 这样的数据才有意思,去掉头尾的节点数据
if (reservedSeat[1] >= 2 && reservedSeat[1] <= 9){
int origin = map.containsKey(reservedSeat[0]) ? map.get(reservedSeat[0]) : 0;
int value = origin | (1 << (reservedSeat[1] - 2));
map.put(reservedSeat[0], value);
}
}
// 一列没有被人预约的
int ans = (n - map.size()) * 2;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
int row = entry.getKey(), bitmask = entry.getValue();
if ((bitmask | left) == left || (bitmask | middle) == middle || (bitmask | right) == right){
++ans;
}
}
return ans;
}
public int maxNumberOfFamilies2(int n, int[][] reservedSeats) {
Map<Integer, List<Integer>> map = new HashMap<>();
for (int[] seat : reservedSeats) {
List<Integer> value = null;
if (map.get(seat[0]) != null){
value = map.get(seat[0]);
} else {
value = new ArrayList<>();
}
value.add(seat[1]);
map.put(seat[0], value);
}
int ans = (n - map.size()) * 2;
for (Map.Entry<Integer, List<Integer>> entry : map.entrySet()) {
boolean left = true, middle = true, right = true;
for (Integer integer : entry.getValue()) {
if (integer >= 2 && integer <= 5){
left = false;
}
if (integer >= 4 && integer <= 7){
middle = false;
}
if (integer >= 6 && integer <= 9){
right = false;
}
}
if (left && right){
ans += 2;
} else if (left || right || middle){
ans += 1;
}
}
return ans;
}
}
力扣:1386. 安排电影院座位
最新推荐文章于 2024-08-25 17:36:33 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)