239 滑动窗口最大值

本文最后更新于:2022年9月6日 早上

给你一个整数数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。

返回滑动窗口中的最大值。

示例 1:

1
2
3
4
5
6
7
8
9
10
11
输入:nums = [1,3,-1,-3,5,3,6,7], k = 3
输出:[3,3,5,5,6,7]
解释:
滑动窗口的位置 最大值
--------------- -----
[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

示例 2:

1
2
输入:nums = [1], k = 1
输出:[1]

示例 3:

1
2
输入:nums = [1,-1], k = 1
输出:[1,-1]

示例 4:

1
2
输入:nums = [9,11], k = 2
输出:[11]

示例 5:

1
2
输入:nums = [4,-2], k = 2
输出:[4]

提示:

  • 1 <= nums.length <= 105
  • -104 <= nums[i] <= 104
  • 1 <= k <= nums.length

Solution

参考:**@sam_2019** 、 @LeetCode官方

  • 单调队列 ,队列的第一个元素即为最大元素
  • 当队列非空,左边界出界时(滑动窗口向右移导致的),更新左边界
  • 当队列非空,将队列中索引对应的元素值比 num 小的移除
  • 更新队列
  • 当索引 i 大于 k-1(已经构成长度为 k 的窗口),更新输出结果

lc239_1.PNG

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# @lc code=start
class Solution:
def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
# 双端队列,保存的是索引值
window = collections.deque()
res = []
for i in range(len(nums)):
while window and nums[window[-1]]<nums[i]:
window.pop()

window.append(i)
if window[0]<=i-k:
window.popleft()

if i>=k-1:
res.append(nums[window[0]])
return res
# @lc code=end

cpp

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
class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
int n = nums.size();
deque<int> q; // 保存的是索引值
for (int i = 0; i < k; ++i) {
while (!q.empty() && nums[i] >= nums[q.back()]) {
q.pop_back();
}
q.push_back(i);
}

vector<int> res;
res.push_back(nums[q.front()]);
for (int i = k; i < n; ++i) {
while (!q.empty() && nums[i] >= nums[q.back()]) {
q.pop_back();
}
q.push_back(i);
while (q.front() <= i-k) {
q.pop_front();
}
res.push_back(nums[q.front()]);
}
return res;
}
};

参考 代码随想录《算法小抄》3.8

  • 实现一个 单调队列 类
  • pop(value):如果窗口移除的元素value等于单调队列的出口元素,那么队列弹出元素,否则不用任何操作
  • push(value):如果push的元素value大于入口元素的数值,那么就将队列入口的元素弹出,直到push元素的数值小于等于队列入口元素的数值为止
  • 时间复杂度是 O(n),空间复杂度是 O(k)
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
class Solution {
private:
// 单调队列
class MonotonicQueue {
public:
deque<int> que;

void pop(int value) {
if (!que.empty() && value == que.front()) {
que.pop_front();
}
}

void push(int value) {
while (!que.empty() && value > que.back()) {
que.pop_back();
}
que.push_back(value);
}

int front() {
return que.front();
}
};

public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
int n = nums.size();
MonotonicQueue que;
vector<int> res;

for (int i = 0; i < k; ++i) {
que.push(nums[i]);
}
res.push_back(que.front());
for (int i = k; i < n; ++i) {
que.pop(nums[i-k]);
que.push(nums[i]);
res.push_back(que.front());
}
return res;
}
};
  • 借助优先队列

image-20210207163823308

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
int n = nums.size();
priority_queue<pair<int, int>> pq;
for (int i = 0; i < k; ++i) {
pq.emplace(nums[i], i);
}

vector<int> res;
res.push_back(pq.top().first);
for (int i = k; i < n; ++i) {
pq.emplace(nums[i], i);
while (pq.top().second <= i-k) {
pq.pop();
}
res.push_back(pq.top().first);
}
return res;
}
};

java

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
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
int n = nums.length;
Deque<Integer> deque = new LinkedList<>(); // 存的是下标
for (int i = 0; i < k; ++i) {
while (!deque.isEmpty() && nums[i] >= nums[deque.peekLast()]) {
deque.pollLast();
}
deque.offerLast(i);
}

int[] res = new int[n - k + 1];
res[0] = nums[deque.peekFirst()];
for (int i = k; i < n; ++i) {
while (!deque.isEmpty() && nums[i] >= nums[deque.peekLast()]) {
deque.pollLast();
}
deque.offerLast(i);
while (deque.peekFirst() <= i - k) {
deque.pollFirst();
}
res[i - k + 1] = nums[deque.peekFirst()];
}
return res;
}
}

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!