-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcinema_seat_allocation.py
66 lines (58 loc) · 2.62 KB
/
cinema_seat_allocation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# https://leetcode.com/problems/cinema-seat-allocation/description/
# 1386. Cinema Seat Allocation
# 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.
from collections import defaultdict
from typing import List
class Solution:
def maxNumberOfFamilies(self, n: int, reservedSeats: List[List[int]]) -> int:
''' Cinema Seat Allocation '''
# l2-5 r6-9 m4-7
output = n * 2
occupied = defaultdict(set)
for el in reservedSeats:
# ВЕСА: l, c, r = 1, 2, 7
if 2 <= el[1] <= 5:
occupied[el[0]].add(1)
if 4 <= el[1] <= 7:
occupied[el[0]].add(2)
elif 6 <= el[1] <= 9:
occupied[el[0]].add(7)
if 4 <= el[1] <= 7:
occupied[el[0]].add(2)
for places in occupied.values():
# occupied[row]:
# =1 >> -1
# =3 >> -1
# =9 >> -1
# =10 >> -2
if sum(places) == 10:
output -= 2
else:
output -= 1
return output
n, reserved = 3, [[1,2],[1,3],[1,8],[2,6],[3,1],[3,10]]
# Output: 4
n, reserved = 2, [[2,1],[1,8],[2,6]]
# Output: 2
n, reserved = 4, [[4,3],[1,4],[4,6],[1,7]]
# Output: 4
Solution().maxNumberOfFamilies(n, reserved)