题目
给定一个数组 nums
和滑动窗口的大小 k
,请找出所有滑动窗口里的最大值。
示例:
输入: 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
提示:
你可以假设 k 总是有效的,在输入数组 不为空 的情况下,1 ≤ k ≤ nums.length
。
注意:本题与主站 239 题相同:https://leetcode-cn.com/problems/sliding-window-maximum/
题解
java
public int[] maxSlidingWindow(int[] nums, int k) {
int len = nums.length;
if (len == 0) {
return new int[0];
}
int[] result = new int[len - k + 1];
int prefixMaxIndex = -1;
for (int i = 0; i < len - k + 1; i++) {
// 前一窗口最大值不在当前窗口内
if (prefixMaxIndex < i) {
int max = Integer.MIN_VALUE;
for (int j = i; j < k + i; j++) {
if (nums[j] > max) {
max = nums[j];
prefixMaxIndex = j;
}
}
result[i] = max;
} else if (nums[prefixMaxIndex] < nums[k + i - 1]) {
// 前一窗口最大值小于当前窗口最后一个值
prefixMaxIndex = k + i - 1;
result[i] = nums[k + i - 1];
} else {
// 前一窗口最大值也是当前窗口最大值
result[i] = nums[prefixMaxIndex];
}
}
return result;
}
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
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