class MaxQueue:
def __init__(self):
self.A = []
self.B = []
def max_value(self) -> int:
if not self.B:
return -1
return self.B[0]
def push_back(self, value: int) -> None:
while self.B and self.B[-1] <value:
self.B.pop()
self.B.append(value)
self.A.append(value)
def pop_front(self) -> int:
if not self.B:
return -1
ans_A = self.A.pop(0)
if ans_A == self.B[0]:
self.B.pop(0)
return ans_A
# Your MaxQueue object will be instantiated and called as such:
# obj = MaxQueue()
# param_1 = obj.max_value()
# obj.push_back(value)
# param_3 = obj.pop_front()
每日一道Leetcode - 剑指 Offer 59 - II. 队列的最大值【模拟|双栈】
最新推荐文章于 2022-07-03 14:23:25 发布