LeetCode-Python-1386. Arrange movie theater seats (array)


As shown in the figure above, there are n rows of seats in the movie theater's viewing hall, with row numbers from 1 to n, and a total of 10 seats in each row, and column numbers from 1 to 10.

Give you the array reservedSeats, which contains all the reserved seats. For example, researvedSeats [i] = [3,8], it means that the 8th seat in line 3 is reserved.

Please return. How many 4 person families can be arranged? A family of 4 people should occupy 4 consecutive seats in the same row. The seats across the aisle (such as [3,3] and [3,4]) are not consecutive seats, but if you can split a 4-person family into two people on each side of the aisle, this is allowed.

 

Example 1:

Input: n = 3, reservedSeats = [[1,2], [1,3], [1,8], [2,6], [3,1], [3,10]]
Output: 4
Explanation: The picture above shows the optimal arrangement plan, a total of 4 families can be arranged. The blue cross indicates the reserved seat, and the orange continuous seat indicates a 4-person family.
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
 

prompt:

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 different from each other.
Passes 1,517 Submits 6,325

Source: LeetCode (LeetCode)
Link: https://leetcode-cn.com/problems/cinema-seat-allocation The
copyright belongs to the deduction network. Please contact the official authorization for commercial reprint, and please indicate the source for non-commercial reprint.

Ideas:

The first row of seats can seat up to two families,

Secondly, only 2345, 4567, 6789 can sit a family.

If both 2345 and 6789 are empty, you can sit down in two families,

If one of 2345 and 6789 is empty, you can sit down with a family.

So as long as we judge the vacancy of 23 45 67 89 in each row, we can know how many families can sit in this row.

In addition, it should be noted that the data size of this question is very large. If the above judgment is performed for each row, it will time out.

So we might as well judge only the behavior of people who have sat down, and sit down directly with two families.

Time complexity: O (K), K is the length of reservedSeats

Space complexity: O (K)

class Solution(object):
    def maxNumberOfFamilies(self, n, reservedSeats):
        """
        :type n: int
        :type reservedSeats: List[List[int]]
        :rtype: int
        """
        from collections import defaultdict
        dic = defaultdict(set)
        res = 0
        usedrow = set()
        for row, seat in reservedSeats:
            dic[row].add(seat)
            usedrow.add(row)
        
        for row in usedrow:
            twothree = 2 not in dic[row] and 3 not in dic[row]
            fourfive = 4 not in dic[row] and 5 not in dic[row]
            sixseven = 6 not in dic[row] and 7 not in dic[row]
            eightnine = 8 not in dic[row] and 9 not in dic[row]

            if twothree and fourfive and sixseven and eightnine:
                res += 2
            elif twothree and fourfive:
                res += 1
            elif fourfive and sixseven:
                res += 1
            elif sixseven and eightnine:
                res += 1
        return res + (n - len(usedrow)) * 2

 

Published 734 original articles · 121 praises · 210,000 views

Guess you like

Origin blog.csdn.net/qq_32424059/article/details/105154951