動機

十分的哭,1 <= n <= 10^9 會不會太大

Problem

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: 4Explanation: 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.

Sol

因為太大,所以不能把所以資料一次讀進來,不然記憶體會爆

所以,要一筆一筆的讀,我們只看有劃位的部分,剩下一定是2組

class Solution:
    def maxNumberOfFamilies(self, n: int, reservedSeats: List[List[int]]) -> int:
        center = int('0001111000',2)
        left = int('0111100000',2)
        right = int('0000011110',2)
        def avail4(avail):
            isLeft = (avail & left == left)
            isRight = (avail & right == right)
            isCenter = (avail & center == center)
            if isLeft and isRight:
                return 2
            elif isLeft or isRight or isCenter:
                return 1
            else:
                return 0
        seats = int('1111111111', 2)
        avails = defaultdict(lambda : seats)
        
        for s,i in reservedSeats:
            avails[s] = avails[s] & ~(1 << (i-1))
        
        return 2*(n-len(avails))+sum(avail4(avail) for avail in avails.values())