Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position.
For example,
Given nums = [1,3,-1,-3,5,3,6,7], and k = 3.
Window position Max
--------------- -----
[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
Therefore, return the max sliding window as [3,3,5,5,6,7].
[分析] 首先要正确理解题意,结果是返回每个长度为k的窗口中的最大值。当窗口滑动一格时如何确定窗口中的最大值呢?直接的方法是遍历,则复杂度是O(k * n),选用最大堆存储窗口,则找到当前堆中最大值时O(1),窗口滑动时,需要添加一个新元素并删除窗口左边界元素,耗费为O(logk),因此总的复杂度是O(n*logk),当k很大时效率提升比较明显
更优的思路是使用Deque, 时间复杂度为O(N),之前都没用过这种数据结构,JDK文档上说ArrayDeque用作stack时比Stack数据结构快,用作queue时比LinkedList快,好东西呀~
参考
https://leetcode.com/discuss/46578/java-o-n-solution-using-deque-with-explanation
// Method 2: O(N), using Deque
public int[] maxSlidingWindow(int[] nums, int k) {
if (nums == null || nums.length == 0)
return new int[0];
int n = nums.length;
int[] max = new int[n - k + 1];
int idx = 0;
Deque<Integer> deque = new ArrayDeque<Integer>();
for (int i = 0; i < n; i++) {
if (!deque.isEmpty() && deque.peek() == i - k)
deque.pollFirst();
while (!deque.isEmpty() && nums[deque.peekLast()] <= nums[i])
deque.pollLast();
deque.offer(i);
if (i >= k - 1) max[idx++] = nums[deque.peek()];
}
return max;
}
// Method 1: O(N * logk), using PriorityQueue
public class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
if (nums == null)
return null;
if (nums.length == 0)
return new int[0];
int n = nums.length;
int[] result = new int[n - k + 1];
PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(k, new LargerComparator());
for (int i = 0; i < k; i++) {
maxHeap.offer(nums[i]);
}
result[0] = maxHeap.peek();
int start = 0;
for (int i = k; i < n; i++) {
maxHeap.offer(nums[i]);
maxHeap.remove(nums[start++]);
result[i - k + 1] = maxHeap.peek();
}
return result;
}
}
class LargerComparator implements Comparator<Integer> {
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
}
分享到:
相关推荐
《leetcode-solutions》,刷算法题,需要有一定的英文阅读能力。。。
IDEA 插件,lettcode刷题,leetcode-editor7.4版本下载进行本地导入(直接将压缩包拖进IDEA即可)
Algorithm-LeetCode-Sol-Res.zip,干净,易懂的解决方案和资源,为leetcode在线判断算法问题。,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
Algorithm-leetcode-spider.zip,leetcode公司,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
leetcode-cli-plugins leetcode-cli 的第 3 方插件。 什么是 如何使用 如何使用 插件 名称 描述 增强的命令 按公司或标签过滤问题 list 不要在同一台计算机上使 Chrome 的会话过期 login 不要在同一台计算机上使 ...
在IDE中解决LeetCode问题,支持leetcode.com与leetcode-cn.com,满足基本的做题需求。 理论上支持: IntelliJ IDEA PhpStorm WebStorm PyCharm RubyMine AppCode CLion GoLand DataGrip Rider MPS Android Studio。
解题思路思路和LeetCode-python 503.下一个更大元素 II一致,只是这里求的是下标的距离,而不是数值倒序搜索,用到栈,栈里存储索引情况1:若栈为
leetcode-cheat 的发布 它是什么 ? 这是一个chrome 扩展,可以帮助您更高效地使用 leetcode。您可以从 重要: leetcode-cheat 现在只支持中文版。 也就是说不完全支持leetcode.com,但是你可以用leetcode-cn.com代替...
leetcode 答案解析 golang解答
~/.vscode/extensions/leetcode.vscode-leetcode-0.17.0/node_modules/vsc-leetcode-cli/bin/leetcode /usr/local/bin/leetcode 修改模板 open ~/.vscode/extensions/leetcode.vscode-leetcode-0.17.0/node_modules/...
leetcode-editor,在ide中做leetcode练习,支持leetcode.com和leetcode-cn.com,以满足练习的基本需求。理论上支持:intellij idea phpstorm webstorm pycharm rubymine appcode clion goland datagrip rider mps ...
leetcode-helper-1.7.1
假设数组长度为N,准备N+1个桶,把max放在N+1号桶,nums中在[min,max)范围上的数放在1~N号桶中,对于1 ~ N号桶的每一个桶来说,负责的区间
leetcode-tag-dynamic programming
然后进入到LeetCode-Spider目录中修改config.json,其中outputDir需要填写该工程的/docs/views文件夹路径 { "username": "aaa", "password": "bbb", "outputDir": "/Users/liuyao/Downloads/LeetCode-Blog-Test/docs...
leetcode-cli 注意:这个存储库是为了临时使用而分叉的。 注意:从 webbrowser 复制 cookie 并使用leetcode user -c可以临时修复不能。 一个享受 leetcode 的高效 cli 工具! 非常感谢 leetcode.com,一个非常棒的...
leetcode-tag-Tree
leetcode-tag-Stack
leetcode-tag-array