-
Notifications
You must be signed in to change notification settings - Fork 0
/
59_2_queue_with_max_method.py
71 lines (58 loc) · 2.16 KB
/
59_2_queue_with_max_method.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
# -*- coding=utf-8 -*-
from collections import deque
class WithMaxQueue():
"""能O(1)时间复杂度返回队列最大值的队列实现:
实现了pop_front以及push_back方法
"""
def __init__(self):
"""初始化,self.__l, self.__r两个参数很重要
params:
self.__queue: 存储数据的队列
self.__shadow: 存储数据队列中最大值的队列
self.__l: 等价于滑动窗口左边界,表示当前队列头部是第几个添加进来的元素
self.__r: 等价于滑动窗口右边界,表示当前队列尾部是第几个添加进来的元素
"""
self.__queue = deque()
self.__shadow = deque()
self.__l, self.__r = 0, 0
def push_back(self, item):
self.__queue.append(item)
self.expand(item)
self.__r += 1
def pop_front(self):
if self.__queue:
self.__queue.popleft()
self.__l += 1
self.shrink()
else:
raise IndexError("pop from emtpy queue")
def expand(self, item):
"""滑动窗口的扩大过程:即当有新元素时,对最大值队列的调整
"""
if not self.__shadow:
self.__shadow.append((self.__r, item))
else:
if item < self.__shadow[-1][-1]:
self.__shadow.append((self.__r, item))
else:
while self.__shadow and item >= self.__shadow[-1][-1]:
self.__shadow.pop()
self.__shadow.append((self.__r, item))
def shrink(self):
"""滑动窗口的缩小过程(窗口左边界右移)即:当弹出元素时,对最大值队列的调整
"""
while self.__shadow and self.__l > self.__shadow[0][0]:
self.__shadow.popleft()
def get_max(self):
return self.__shadow[0][-1]
if __name__ == "__main__":
ex = WithMaxQueue()
num = [2, 3, 4, 2, 6, 2, 5, 1]
# The anster is 4, 4, 6, 6, 6, 5
for i in range(3):
ex.push_back(num[i])
print(ex.get_max())
for i in range(3, len(num)):
ex.push_back(num[i])
ex.pop_front()
print(ex.get_max())