forked from Garvit244/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
239.py
30 lines (25 loc) · 766 Bytes
/
239.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
from collections import deque
class Solution(object):
def maxSlidingWindow(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""
if len(nums) == 0:
return []
q = deque()
for i in range(k):
while q and nums[i] >= nums[q[-1]]:
q.pop()
q.append(i)
result = []
for i in range(k, len(nums)):
result.append(nums[q[0]])
while q and q[0] <= i-k:
q.popleft()
while q and nums[i] >= nums[q[-1]]:
q.pop()
q.append(i)
result.append(nums[q[0]])
return result