LeetCode 1386. Cinema Seat Allocation
考点 | 难度 |
---|---|
Greedy | Easy |
题目
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.
思路
用dictionary记录左中右有没有被占领。每一行最多fit两组人,如果左中右都被占总数-2,有任何一个没有被占总数-1。
答案
class Solution:
def maxNumberOfFamilies(self, n, reservedSeats):
seats = collections.defaultdict(set)
for i,j in reservedSeats:
if j in [2,3,4,5]:
seats[i].add(0)
if j in [4,5,6,7]:
seats[i].add(1)
if j in [6,7,8,9]:
seats[i].add(2)
res = 2*n
for i in seats:
if len(seats[i]) == 3:
res -= 2
else:
res -= 1
return res