-
Notifications
You must be signed in to change notification settings - Fork 481
/
1172.py
26 lines (23 loc) · 856 Bytes
/
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
import heapq
class DinnerPlates:
def __init__(self, capacity: int):
self.data = []
self.q = []
self.n = capacity
def push(self, val: int) -> None:
while self.q and self.q[0] < len(self.data) and len(self.data[self.q[0]]) == self.n:
heapq.heappop(self.q)
if not self.q:
heapq.heappush(self.q, len(self.data))
if self.q[0] == len(self.data):
self.data.append([])
self.data[self.q[0]].append(val)
def pop(self) -> int:
while self.data and not self.data[-1]:
self.data.pop()
return self.popAtStack(len(self.data) - 1)
def popAtStack(self, index: int) -> int:
if 0 <= index < len(self.data) and self.data[index]:
heapq.heappush(self.q, index)
return self.data[index].pop()
return -1