-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path1172.py
81 lines (71 loc) · 2.77 KB
/
1172.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
__________________________________________________________________________________________________
sample 452 ms submission
class DinnerPlates:
def __init__(self, capacity: int):
self.dat, self.pq, self.pool, self.c = [], [], set(), capacity
def push(self, val: int) -> None:
if not self.pool:
self.dat.append([val])
if len(self.dat[-1]) < self.c:
self.pool.add(len(self.dat) - 1)
heapq.heappush(self.pq, len(self.dat) - 1)
else:
while self.pq[0] not in self.pool:
heapq.heappop(self.pq)
self.dat[self.pq[0]].append(val)
if len(self.dat[self.pq[0]]) == self.c:
self.pool.remove(self.pq[0])
heapq.heappop(self.pq)
def pop(self) -> int:
if self.dat:
ans = self.dat[-1].pop()
if not self.dat[-1]:
if len(self.dat) in self.pool:
self.pool.remove(len(self.dat))
self.pool.add(len(self.dat))
self.dat.pop()
return ans
return -1
def popAtStack(self, index: int) -> int:
if index >= len(self.dat) or not self.dat[index]:
return -1
if index not in self.pool:
self.pool.add(index)
heapq.heappush(self.pq, index)
return self.dat[index].pop()
# Your DinnerPlates object will be instantiated and called as such:
# obj = DinnerPlates(capacity)
# obj.push(val)
# param_2 = obj.pop()
# param_3 = obj.popAtStack(index)
__________________________________________________________________________________________________
class DinnerPlates:
def __init__(self, capacity: int):
self.queue = []
self.c = capacity
self.emp = [] #the non-full stacks, if the same index appears twice, that means it has two empty positions in this stack.
def push(self, val: int) -> None:
if self.emp:
l = heapq.heappop(self.emp)
self.queue[l] += [val]
return
if len(self.queue)>0 and len(self.queue[-1]) < self.c:
self.queue[-1] += [val]
return
self.queue += [[val]]
def pop(self) -> int:
while len(self.queue) > 0 and not self.queue[-1]:
self.queue.pop()
if self.queue:
res = self.queue[-1][-1]
self.queue[-1].pop()
return res
return -1
def popAtStack(self, index: int) -> int:
if index < len(self.queue) and len(self.queue[index]) > 0:
res = self.queue[index][-1]
self.queue[index].pop()
heapq.heappush(self.emp,index)
return res
return -1
__________________________________________________________________________________________________