-
Notifications
You must be signed in to change notification settings - Fork 7
/
windows.py
219 lines (179 loc) · 5.43 KB
/
windows.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# -*- coding: utf-8 -*-
# author: Xiguang Liu<[email protected]>
# 2018-05-05 18:46
# 题目描述:https://www.nowcoder.com/practice/1624bc35a45c42c0bc17d17fa0cba788?tpId=13&tqId=11217&rp=1&ru=%2Fta%2Fcoding-interviews&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking
class Solution:
"""
可以使用 O(1) 空间支持 Max 操作的栈
使用两个栈可以实现一个队列
因为窗口是从左到右滑动,其中的元素是先进先出的,符合队列的行为
"""
def maxInWindows(self, num, size):
queue = MaxQueue()
ret = []
if len(num) < size or size < 1:
return ret
for i in range(size - 1):
queue.enqueue(num[i])
for i in range(size - 1, len(num)):
queue.enqueue(num[i])
ret.append(queue.max())
queue.dequeue()
return ret
class MaxStack:
def __init__(self):
self.stack = []
self.maxElem = None
def push(self, x):
if self.stack:
if x > self.maxElem:
y = 2 * x - self.maxElem
self.stack.append(y)
self.maxElem = x
# 经过上述操作,y > self.maxElem
else:
self.stack.append(x)
else:
self.stack.append(x)
self.maxElem = x
def pop(self):
if not self.stack:
raise EmptyStack
y = self.stack.pop()
if y > self.maxElem:
ret = self.maxElem
self.maxElem = 2 * self.maxElem - y
return ret
else:
return y
def top(self):
if not self.stack:
raise EmptyStack
y = self.stack[-1]
if y > self.maxElem:
return self.maxElem
else:
return y
def max(self):
if not self.stack:
raise EmptyStack
return self.maxElem
def isEmpty(self):
return len(self.stack) == 0
class MaxQueue:
"""
使用两个支持 max 操作的栈实现队列
"""
def __init__(self):
self.write = MaxStack()
self.read = MaxStack()
def enqueue(self, x):
self.write.push(x)
def dequeue(self):
if self.read.isEmpty():
while not self.write.isEmpty():
t = self.write.pop()
self.read.push(t)
if self.read.isEmpty():
raise EmptyQueue
return self.read.pop()
def max(self):
try:
writeMax = self.write.max()
except EmptyStack:
writeMax = None
try:
readMax = self.read.max()
except EmptyStack:
readMax = None
if not writeMax and not readMax:
raise EmptyQueue
if writeMax and readMax:
return max(writeMax, readMax)
return writeMax or readMax
class EmptyStack(Exception):
pass
class EmptyQueue(Exception):
pass
class Solution2:
"""
使用双端开口的队列实现
"""
def maxInWindows(self, num, size):
q = CircleQueue(size)
ret = []
if len(num) < size or size < 1:
return ret
for i in range(size - 1):
# 将元素的下标而不是元素值加入到队列中
while q.len() > 0 and num[q.tailElem()] <= num[i]:
q.removeTailElem()
q.enqueue(i)
for i in range(size - 1, len(num)):
x = num[i]
while q.len() > 0 and num[q.tailElem()] <= x:
q.removeTailElem()
q.enqueue(i)
ret.append(max(num[q.frontElem()], x))
if i - q.frontElem() >= size - 1:
q.deque()
return ret
class CircleQueue:
"""
底层使用数组实现的循环队列
队列双端开头,既可以从队头去元素,也可以从队尾取元素
"""
def __init__(self, size):
self.size = size + 1
self.maxLen = self.size - 1
self.q = [None] * self.size
self.front, self.tail = 0, 0
def enqueue(self, x):
if self.len() == self.maxLen:
raise FullQueue
self.q[self.tail] = x
self.tail = (self.tail + 1) % self.size
def deque(self):
if self.len() == 0:
raise EmptyQueue
x = self.q[self.front]
self.q[self.front] = None
self.front = (self.front + 1) % self.size
return x
def len(self):
return (self.tail - self.front + self.size) % self.size
def frontElem(self):
if self.len() == 0:
raise EmptyQueue
return self.q[self.front]
def tailElem(self):
if self.len() == 0:
raise EmptyQueue
return self.q[self.tail - 1]
def removeTailElem(self):
"""
移除队尾元素
:return:
"""
if self.len() == 0:
raise EmptyQueue
x = self.q[self.tail - 1]
self.tail = (self.tail - 1) % self.size
self.q[self.tail] = None
return x
class FullQueue(Exception):
pass
import unittest
class TestCase(unittest.TestCase):
def setUp(self):
self.s = Solution2()
def test_1(self):
nums = [2, 3, 4, 2, 6, 2, 5, 1]
size = 3
r = self.s.maxInWindows(nums, size)
self.assertEqual([4, 4, 6, 6, 6, 5], r)
def test_2(self):
nums = [1, 3, -1, -3, 5, 3, 6, 7]
size = 3
r = self.s.maxInWindows(nums, size)
self.assertEqual([3, 3, 5, 5, 6, 7], r)