leet-code/src/main/java/leetcode/editor/cn/SlidingWindowMaximum.java
huangge1199@hotmail.com 6292e3eef8 239:滑动窗口最大值
2021-07-12 21:34:42 +08:00

97 lines
2.3 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//给你一个整数数组 nums有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位
//。
//
// 返回滑动窗口中的最大值。
//
//
//
// 示例 1
//
//
//输入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
//
//
//输入nums = [1], k = 1
//输出:[1]
//
//
// 示例 3
//
//
//输入nums = [1,-1], k = 1
//输出:[1,-1]
//
//
// 示例 4
//
//
//输入nums = [9,11], k = 2
//输出:[11]
//
//
// 示例 5
//
//
//输入nums = [4,-2], k = 2
//输出:[4]
//
//
//
// 提示:
//
//
// 1 <= nums.length <= 105
// -104 <= nums[i] <= 104
// 1 <= k <= nums.length
//
// Related Topics 队列 数组 滑动窗口 单调队列 堆(优先队列)
// 👍 1062 👎 0
package leetcode.editor.cn;
import java.util.LinkedList;
//239:滑动窗口最大值
class SlidingWindowMaximum {
public static void main(String[] args) {
//测试代码
Solution solution = new SlidingWindowMaximum().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
if (nums == null || nums.length < 2) return nums;
LinkedList<Integer> queue = new LinkedList<>();
int[] result = new int[nums.length - k + 1];
for (int i = 0; i < nums.length; i++) {
while (!queue.isEmpty() && nums[queue.peekLast()] <= nums[i]) {
queue.pollLast();
}
queue.addLast(i);
if (queue.peek() <= i - k) {
queue.poll();
}
if (i + 1 >= k) {
result[i + 1 - k] = nums[queue.peek()];
}
}
return result;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}