forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcinema-seat-allocation.py
62 lines (57 loc) · 1.69 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
# Time: O(n)
# Space: O(n)
import collections
class Solution(object):
def maxNumberOfFamilies(self, n, reservedSeats):
"""
:type n: int
:type reservedSeats: List[List[int]]
:rtype: int
"""
lookup = collections.defaultdict(lambda: [False]*3)
for r, c in reservedSeats:
if 2 <= c <= 5:
lookup[r][0] = True
if 4 <= c <= 7:
lookup[r][1] = True
if 6 <= c <= 9:
lookup[r][2] = True
result = 2*n
for a, b, c in lookup.itervalues():
if not a and not c:
continue
if not a or not b or not c:
result -= 1
continue
result -= 2
return result
# Time: O(nlogn)
# Space: O(1)
class Solution2(object):
def maxNumberOfFamilies(self, n, reservedSeats):
"""
:type n: int
:type reservedSeats: List[List[int]]
:rtype: int
"""
reservedSeats.sort()
result, i = 2*n, 0
while i < len(reservedSeats):
reserved = [False]*3
curr = reservedSeats[i][0]
while i < len(reservedSeats) and reservedSeats[i][0] == curr:
_, c = reservedSeats[i]
if 2 <= c <= 5:
reserved[0] = True
if 4 <= c <= 7:
reserved[1] = True
if 6 <= c <= 9:
reserved[2] = True
i += 1
if not reserved[0] and not reserved[2]:
continue
if not all(reserved):
result -= 1
continue
result -= 2
return result