LeetCode | 1386. Cinema Seat Allocation安排電影院座位【Python】

LeetCode 1386. Cinema Seat Allocation安排電影院座位【Medium】【Python】【哈希表】

Problem

LeetCode

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 families you can allocate on the cinema seats. A four-person family occupies fours seats in one row, that are next to each other. Seats across an aisle (such as [3,3] and [3,4]) are not considered to be next to each other, however, It is permissible for the four-person family to be separated by an aisle, but in that case, exactly two people have to sit on each side of the aisle.

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 families, where seats mark with blue are already reserved and contiguous seats mark with orange are for one 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

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.

問題

力扣

如上圖所示,電影院的觀影廳中有 n 行座位,行編號從 1 到 n ,且每一行內總共有 10 個座位,列編號從 1 到 10 。

給你數組 reservedSeats ,包含所有已經被預約了的座位。比如說,researvedSeats[i]=[3,8] ,它表示第 3 行第 8 個座位被預約了。

請你返回 最多能安排多少個 4 人家庭 。4 人家庭要佔據 同一行內連續 的 4 個座位。隔着過道的座位(比方說 [3,3] 和 [3,4])不是連續的座位,但是如果你可以將 4 人家庭拆成過道兩邊各坐 2 人,這樣子是允許的。

示例 1:

輸入:n = 3, reservedSeats = [[1,2],[1,3],[1,8],[2,6],[3,1],[3,10]]
輸出:4
解釋:上圖所示是最優的安排方案,總共可以安排 4 個家庭。藍色的叉表示被預約的座位,橙色的連續座位表示一個 4 人家庭。

示例 2:

輸入:n = 2, reservedSeats = [[2,1],[1,8],[2,6]]
輸出:2

示例 3:

輸入:n = 4, reservedSeats = [[4,3],[1,4],[4,6],[1,7]]
輸出:4

提示:

  • 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
  • 所有 reservedSeats[i] 都是互不相同的。

思路

哈希表

正面考慮容易超時/超內存,可以從反面來考慮。
統計出被預約的位置,然後反向篩選一下可以坐的位置。
Python3代碼
from typing import List

class Solution:
    def maxNumberOfFamilies(self, n: int, reservedSeats: List[List[int]]) -> int:
        left, right, mid = set(), set(), set()
        count = 0
        
        # 統計被預約的位置
        for r, c in reservedSeats:
            if r in left and r in right and r in mid:
                continue
            if c < 6 and c > 1:
                left.add(r)
            if c < 10 and c > 5:
                right.add(r)
            if c < 8 and c > 3:
                mid.add(r)
        for i in (left|right|mid):
            # 預約位置在兩邊:1 or 10
            if i not in left and i not in right:
                count += 2
            # 預約位置在左邊/右邊:2 or 3 or 8 or 9
            elif i not in mid:
                count += 1
            # 預約位置在中間:4 or 5 or 6 or 7
            elif i not in left or i not in right:
                count += 1
        # 反向篩選一下,沒有被預約的行最多可以坐兩家人
        count += 2*(n - len(left|right|mid))
        return count

GitHub鏈接

Python

參考

Python 反向篩選

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章