-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxQueue.py
More file actions
37 lines (30 loc) · 918 Bytes
/
Copy pathMaxQueue.py
File metadata and controls
37 lines (30 loc) · 918 Bytes
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
# LeetCode
# MaxQueue
# Created by Yigang Zhou on 2020/9/17.
# Copyright © 2020 Yigang Zhou. All rights reserved.
import collections
class MaxQueue:
def __init__(self):
self.q = collections.deque()
self.q_max = collections.deque()
def max_value(self) -> int:
if self.q_max:
return self.q_max[0]
return -1
def push_back(self, value: int) -> None:
self.q.append(value)
while self.q_max and self.q_max[-1] < value:
self.q_max.pop()
self.q_max.append(value)
def pop_front(self) -> int:
if not self.q:
return -1
e = self.q.popleft()
if self.q_max and self.q_max[0] == e:
self.q_max.popleft()
return e
# 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()