Skip to content

Latest commit

 

History

History
156 lines (127 loc) · 4.86 KB

File metadata and controls

156 lines (127 loc) · 4.86 KB

中文文档

Description

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.

 

Example 1:

Input: n = 3, reservedSeats = [[1,2],[1,3],[1,8],[2,6],[3,1],[3,10]]
Output: 4
Explanation: The figure above shows the optimal allocation for four groups, where seats mark with blue are already reserved and contiguous seats mark with orange are for one group.

Example 2:

Input: n = 2, reservedSeats = [[2,1],[1,8],[2,6]]
Output: 2

Example 3:

Input: n = 4, reservedSeats = [[4,3],[1,4],[4,6],[1,7]]
Output: 4

 

Constraints:

  • 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
  • All reservedSeats[i] are distinct.

Solutions

Python3

class Solution:
    def maxNumberOfFamilies(self, n: int, reservedSeats: List[List[int]]) -> int:
        m = defaultdict(int)
        for i, j in reservedSeats:
            m[i] = m[i] | (1 << (10 - j))
        masks = (0b0111100000, 0b0000011110, 0b0001111000)
        ans = (n - len(m)) << 1
        for v in m.values():
            for mask in masks:
                if (v & mask) == 0:
                    v |= mask
                    ans += 1
        return ans

Java

class Solution {
    public int maxNumberOfFamilies(int n, int[][] reservedSeats) {
        Map<Integer, Integer> m = new HashMap<>();
        for (int[] e : reservedSeats) {
            int i = e[0], j = 10 - e[1];
            int v = m.getOrDefault(i, 0);
            v |= 1 << j;
            m.put(i, v);
        }
        int[] masks = {0b0111100000, 0b0000011110, 0b0001111000};
        int ans = (n - m.size()) << 1;
        for (int v : m.values()) {
            for (int mask : masks) {
                if ((v & mask) == 0) {
                    v |= mask;
                    ++ans;
                }
            }
        }
        return ans;
    }
}

C++

class Solution {
public:
    int maxNumberOfFamilies(int n, vector<vector<int>>& reservedSeats) {
        unordered_map<int, int> m;
        for (auto& e : reservedSeats) {
            int i = e[0], j = 10 - e[1];
            m[i] |= (1 << j);
        }
        vector<int> masks = {0b0111100000, 0b0000011110, 0b0001111000};
        int ans = (n - m.size()) << 1;
        for (auto& [_, v] : m) {
            for (int& mask : masks) {
                if ((v & mask) == 0) {
                    v |= mask;
                    ++ans;
                }
            }
        }
        return ans;
    }
};

Go

func maxNumberOfFamilies(n int, reservedSeats [][]int) int {
	m := map[int]int{}
	for _, e := range reservedSeats {
		i, j := e[0], 10-e[1]
		m[i] |= 1 << j
	}
	masks := []int{0b0111100000, 0b0000011110, 0b0001111000}
	ans := (n - len(m)) << 1
	for _, v := range m {
		for _, mask := range masks {
			if (v & mask) == 0 {
				v |= mask
				ans++
			}
		}
	}
	return ans
}

...