Skip to content

Commit 00cc4a2

Browse files
committed
Create 86. 滑动窗口最大值.md
1 parent f01faf1 commit 00cc4a2

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

86. 滑动窗口最大值.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
***一个整数数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。滑动窗口每次只向右移动一位,返回滑动窗口中的最大值。***
2+
3+
```
4+
输入:nums = [1,3,-1,-3,5,3,6,7], k = 3
5+
6+
输出:[3,3,5,5,6,7]
7+
```
8+
9+
```
10+
class Solution(object):
11+
def maxSlidingWindow(self, nums, k):
12+
"""
13+
:type nums: List[int]
14+
:type k: int
15+
:rtype: List[int]
16+
"""
17+
#递减队列
18+
res = []
19+
queue = []
20+
n = len(nums)
21+
for i in range(n):
22+
while (queue and nums[i] > nums[queue[-1]]):
23+
queue.pop()
24+
queue.append(i)
25+
26+
if i - queue[0] >= k:
27+
queue.pop(0)
28+
29+
if i >= k-1:
30+
res.append(nums[queue[0]])
31+
32+
return res
33+
```

0 commit comments

Comments
 (0)