forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexam-room.py
91 lines (80 loc) · 3.52 KB
/
exam-room.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Time: seat: O(logn) on average,
# leave: O(logn)
# Space: O(n)
import heapq
LEN, POS, L, R = range(4)
LEFT, RIGHT = range(2)
class ExamRoom(object):
def __init__(self, N):
"""
:type N: int
"""
self.__num = N
self.__max_heap = [(-self.__num, 0, -1, self.__num)]
self.__seats = {-1: [-1, self.__num],
self.__num: [-1, self.__num]}
def seat(self):
"""
:rtype: int
"""
while self.__max_heap[0][L] not in self.__seats or \
self.__max_heap[0][R] not in self.__seats or \
self.__seats[self.__max_heap[0][L]][RIGHT] != \
self.__max_heap[0][R] or \
self.__seats[self.__max_heap[0][R]][LEFT] != \
self.__max_heap[0][L]:
heapq.heappop(self.__max_heap) # lazy deletion
curr = heapq.heappop(self.__max_heap)
if curr[L] == -1 and curr[R] == self.__num:
heapq.heappush(self.__max_heap, (-(curr[R]-1),
curr[R]-1,
curr[L]+1, curr[R]))
elif curr[L] == -1:
heapq.heappush(self.__max_heap, (-(curr[R]//2),
curr[R]//2,
curr[L]+1, curr[R]))
elif curr[R] == self.__num:
heapq.heappush(self.__max_heap, (-((curr[R]-1-curr[L])//2),
(curr[R]-1-curr[L])//2+curr[L],
curr[L], curr[R]-1))
else:
heapq.heappush(self.__max_heap, (-((curr[POS]-curr[L])//2),
(curr[POS]-curr[L])//2+curr[L],
curr[L], curr[POS]))
heapq.heappush(self.__max_heap, (-((curr[R]-curr[POS])//2),
(curr[R]-curr[POS])//2+curr[POS],
curr[POS], curr[R]))
self.__seats[curr[POS]] = [curr[L], curr[R]]
self.__seats[curr[L]][RIGHT] = curr[POS]
self.__seats[curr[R]][LEFT] = curr[POS]
return curr[POS]
def leave(self, p):
"""
:type p: int
:rtype: void
"""
neighbors = self.__seats[p]
self.__seats.pop(p)
if neighbors[LEFT] == -1 and neighbors[RIGHT] == self.__num:
heapq.heappush(self.__max_heap,
(-neighbors[RIGHT],
neighbors[LEFT]+1,
neighbors[LEFT], neighbors[RIGHT]))
elif neighbors[LEFT] == -1:
heapq.heappush(self.__max_heap,
(-neighbors[RIGHT],
neighbors[LEFT]+1,
neighbors[LEFT], neighbors[RIGHT]))
elif neighbors[RIGHT] == self.__num:
heapq.heappush(self.__max_heap,
(-(neighbors[RIGHT]-1-neighbors[LEFT]),
neighbors[RIGHT]-1,
neighbors[LEFT], neighbors[RIGHT]))
else:
heapq.heappush(self.__max_heap,
(-((neighbors[RIGHT]-neighbors[LEFT])//2),
(neighbors[RIGHT]-neighbors[LEFT])//2 +
neighbors[LEFT],
neighbors[LEFT], neighbors[RIGHT]))
self.__seats[neighbors[LEFT]][RIGHT] = neighbors[RIGHT]
self.__seats[neighbors[RIGHT]][LEFT] = neighbors[LEFT]