-
Notifications
You must be signed in to change notification settings - Fork 43
/
sliding-window-maximum.py
350 lines (306 loc) · 9.35 KB
/
sliding-window-maximum.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
"""
239. Sliding Window Maximum
Hard
You are given an array of integers nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position.
Return the max sliding window.
Example 1:
Input: nums = [1,3,-1,-3,5,3,6,7], k = 3
Output: [3,3,5,5,6,7]
Explanation:
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 3
1 [3 -1 -3] 5 3 6 7 3
1 3 [-1 -3 5] 3 6 7 5
1 3 -1 [-3 5 3] 6 7 5
1 3 -1 -3 [5 3 6] 7 6
1 3 -1 -3 5 [3 6 7] 7
Example 2:
Input: nums = [1], k = 1
Output: [1]
Example 3:
Input: nums = [1,-1], k = 1
Output: [1,-1]
Example 4:
Input: nums = [9,11], k = 2
Output: [11]
Example 5:
Input: nums = [4,-2], k = 2
Output: [4]
Constraints:
1 <= nums.length <= 105
-104 <= nums[i] <= 104
1 <= k <= nums.length
"""
# V0
# IDEA : DEQUE
class Solution:
def maxSlidingWindow(self, nums, k):
q = collections.deque()
ans = []
for i in range(len(nums)):
while q and nums[q[-1]] <= nums[i]:
q.pop()
q.append(i)
if q[0] == i - k:
q.popleft()
if i >= k - 1:
ans.append(nums[q[0]])
return ans
# V1
# http://bookshadow.com/weblog/2015/07/18/leetcode-sliding-window-maximum/
# IDEA : DEQUE
class Solution:
# @param {integer[]} nums
# @param {integer} k
# @return {integer[]}
def maxSlidingWindow(self, nums, k):
dq = collections.deque()
ans = []
for i in range(len(nums)):
while dq and nums[dq[-1]] <= nums[i]:
dq.pop()
dq.append(i)
if dq[0] == i - k:
dq.popleft()
if i >= k - 1:
ans.append(nums[dq[0]])
return ans
### Test case : dev
# V1'
# IDEA : heapq
# https://leetcode.com/problems/sliding-window-maximum/discuss/65957/Python-solution-with-detailed-explanation
import heapq as h
class Solution(object):
def get_next_max(self, heap, start):
while True:
x,idx = h.heappop(heap)
if idx >= start:
return x*-1, idx
def maxSlidingWindow(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""
if k == 0:
return []
heap = []
for i in range(k):
h.heappush(heap, (nums[i]*-1, i))
result, start, end = [], 0, k-1
while end < len(nums):
x, idx = self.get_next_max(heap, start)
result.append(x)
h.heappush(heap, (x*-1, idx))
start, end = start + 1, end + 1
if end < len(nums):
h.heappush(heap, (nums[end]*-1, end))
return result
# V1''
# IDEA : heapq
# https://leetcode.com/problems/sliding-window-maximum/discuss/65957/Python-solution-with-detailed-explanation
from collections import deque
class Solution(object):
def add_to_dq(self, dq, nums, i):
while len(dq) and nums[dq[-1]] <= nums[i]:
dq.pop()
dq.append(i)
return
def maxSlidingWindow(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""
if k == 0:
return []
dq = deque()
for i in range(k):
self.add_to_dq(dq, nums, i)
result, start, end = [], 0, k-1
while end < len(nums):
while True:
if dq[0] >= start:
result.append(nums[dq[0]])
break
else:
dq.popleft()
start, end = start+1,end+1
if end < len(nums):
self.add_to_dq(dq, nums, end)
return result
# V1'''
# IDEA : Use a Hammer (Bruteforce)
# https://leetcode.com/problems/sliding-window-maximum/solution/
class Solution:
def maxSlidingWindow(self, nums: 'List[int]', k: 'int') -> 'List[int]':
n = len(nums)
if n * k == 0:
return []
return [max(nums[i:i + k]) for i in range(n - k + 1)]
# V1''''
# IDEA : Deque
# https://leetcode.com/problems/sliding-window-maximum/solution/
from collections import deque
class Solution:
def maxSlidingWindow(self, nums: 'List[int]', k: 'int') -> 'List[int]':
# base cases
n = len(nums)
if n * k == 0:
return []
if k == 1:
return nums
def clean_deque(i):
# remove indexes of elements not from sliding window
if deq and deq[0] == i - k:
deq.popleft()
# remove from deq indexes of all elements
# which are smaller than current element nums[i]
while deq and nums[i] > nums[deq[-1]]:
deq.pop()
# init deque and output
deq = deque()
max_idx = 0
for i in range(k):
clean_deque(i)
deq.append(i)
# compute max in nums[:k]
if nums[i] > nums[max_idx]:
max_idx = i
output = [nums[max_idx]]
# build output
for i in range(k, n):
clean_deque(i)
deq.append(i)
output.append(nums[deq[0]])
return output
# V1''''''
# IDEA : Dynamic programming
# https://leetcode.com/problems/sliding-window-maximum/solution/
class Solution:
def maxSlidingWindow(self, nums: 'List[int]', k: 'int') -> 'List[int]':
n = len(nums)
if n * k == 0:
return []
if k == 1:
return nums
left = [0] * n
left[0] = nums[0]
right = [0] * n
right[n - 1] = nums[n - 1]
for i in range(1, n):
# from left to right
if i % k == 0:
# block start
left[i] = nums[i]
else:
left[i] = max(left[i - 1], nums[i])
# from right to left
j = n - i - 1
if (j + 1) % k == 0:
# block end
right[j] = nums[j]
else:
right[j] = max(right[j + 1], nums[j])
output = []
for i in range(n - k + 1):
output.append(max(left[i + k - 1], right[i]))
return output
# V1'''''''
# IDEA : DEQUE
# https://leetcode.com/problems/sliding-window-maximum/discuss/823635/Python-Intuitive-Solution
from collections import deque
class Solution:
def maxSlidingWindow(self, nums, k):
if k > len(nums) or not nums:
return []
ret = []
q = deque()
for i in range(len(nums)):
# remove everything from the back that is <= the current num
while q and q[-1][0] <= nums[i]:
q.pop()
# add the new num to the back
q.append((nums[i], i))
# remove everything from the front if it's not in the window
while q[0][1] <= i - k:
q.popleft()
# start adding results to output array once we have our first size k window
if i >= k - 1:
ret.append(q[0][0])
return ret
# V1''''''''''''
# IDEA : monoclic queue
# https://leetcode.com/problems/sliding-window-maximum/discuss/952539/Python-mono-queue
from collections import deque
class Solution:
def maxSlidingWindow(self, nums, k):
ans = []
mono = deque([])
for i,e in enumerate(nums):
if mono and i-mono[0][0]>=k:
mono.popleft()
while mono and mono[-1][1]<e:
mono.pop()
mono.append((i,e))
if i>=k-1:
ans.append(mono[0][1])
return ans
# V1'''''''''''
# https://blog.csdn.net/PKU_Jade/article/details/77934644
# IDEA : DEQUE
class Solution(object):
def maxSlidingWindow(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""
if not nums:
return []
from collections import deque
dq = deque()
n = len(nums)
ans = []
for i in range(k-1):
# push k-1 elements into queue
while len(dq) > 0:
if nums[dq[-1]] <= nums[i]:
dq.pop()
else:
break
dq.append(i)
for i in range(k-1, n):
while len(dq) > 0:
if nums[dq[-1]] <= nums[i]:
dq.pop()
else:
break
dq.append(i)
while dq[0] < i-k+1:
dq.popleft()
ans.append(nums[dq[0]])
return ans
# V1''''''''''''
# https://blog.csdn.net/fuxuemingzhu/article/details/100828798
# IDEA : DEQUE
class Solution(object):
def maxSlidingWindow(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""
que = collections.deque() # [[i, num]]
res = []
for i, num in enumerate(nums):
if que and i - que[0][0] >= k:
que.popleft()
while que and que[-1][1] <= num:
que.pop()
que.append([i, num])
if i >= k - 1:
res.append(que[0][1])
return res
# V2