力扣 239. 滑动窗口最大值

单调队列 N/A

1
2
3
4
5
6
7
8
9
10
def maxSlidingWindow(nums, k):
queue, ret = [], []
for i in range(len(nums)):
while queue and queue[-1] < nums[i]: queue.pop()
queue.append(nums[i])
if i >= k - 1:
if i >= k and nums[i - k] == queue[0]:
queue.pop(0)
ret.append(queue[0])
return ret

修改单调队列格式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import collections

def maxSlidingWindow(nums, k):
queue, ret = collections.deque(), []
for i in range(len(nums)):
while queue and queue[-1] < nums[i]:
queue.pop()
queue.append(nums[i])
if i >= k - 1:
if i >= k and nums[i - k] == queue[0]:
queue.popleft()
ret.append(queue[0])
# print(queue)
return ret
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2022 eightyninth
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信