leetcode面试准备:Sliding Window Maximum
leetcode面试准备:Sliding Window Maximum
1 题目
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].
Note:
You may assume k is always valid, ie: 1 ≤ k ≤ input array's size for non-empty array.
Hint:
- How about using a data structure such as deque (double-ended queue)?
- The queue size need not be the same as the window’s size.
- Remove redundant elements and the queue should store only elements that need to be considered.
接口:public int[] maxSlidingWindow(int[] nums, int k)
2 思路
Brute force solution is O(nw)
Use heap, when window moves, delete the first one in the window, add the next one into the window. The run time complexity is O(n lg w).
Use double-ended queue.
The obvious solution with run time complexity of O(nw) is definitely not efficient enough. Every time the window is moved, we have to search for the maximum from w elements in the window.
We need a data structure where we can store the candidates for maximum value in the window and discard the element, which are outside the boundary of window. For this, we need a data structure in which we can edit at both the ends, front and back. Deque is a perfect candidate for this problem.
We are trying to find a way in which, we need not search for maximum element among all in the window. We will make sure that the largest element in the window would always appear in the front of the queue.
While traversing the array in forward direction if we find a window where element A[i] > A[j] and i > j, we can surely say that A[j], will not be the maximum element for this and succeeding windows. So there is no need of storing j in the queue and we can discard A[j] forever.
For example, if the current queue has the elements: [4 13 9], and a new element in the window has the element 15. Now, we can empty the queue without considering elements 4, 13, and 9, and insert only element 15 into the queue.
3 代码
// 暴力解法:采用左右边界滑动窗口
// Time:O(n*k)
public int[] maxSlidingWindow1(int[] nums, int k) {
int len = nums.length;
if (len == 0)
return new int[0];
int left = 0, right = k - 1;
int[] res = new int[len - k + 1];
while (right < len) {
res[left] = nums[left];
for (int i = left + 1; i <= right; i++) {
res[left] = Math.max(res[left], nums[i]);
}
left++;
right++;
}
return res;
}
// 用TreeSet实现堆,减少查询最大值的效率,降低了复杂度。
// Wrong Answer:无法解决重复元素的问题。
// Time:O(nlogK) Space:O(k)
public int[] maxSlidingWindow2(int[] nums, int k) {
int len = nums.length;
if (len == 0)
return new int[0];
int[] res = new int[len - k + 1];
TreeSet<Integer> heap = new TreeSet<Integer>();
for (int i = 0; i < k - 1; i++) {
heap.add(nums[i]);
}
for (int i = k - 1; i < len; i++) {
heap.add(nums[i]);
int index = i - k + 1;
res[index] = heap.last();
heap.remove(nums[index]);
}
return res;
}
// 用双端队列来解决,时间复杂度降到了O(n)
// Time:O(n) Space:O(k)
public int[] maxSlidingWindow(int[] nums, int k) {
int len = nums.length;
if (len == 0)
return new int[0];
int[] res = new int[len - k + 1];
Deque<Integer> dq = new LinkedList<Integer>();
for (int i = 0; i < len; i++) {
int data = nums[i];
while (!dq.isEmpty() && dq.getLast() < data) {
dq.removeLast();
}
dq.add(data);
if (i < k - 1) {
continue;
}
int index = i - k + 1;
res[index] = dq.getFirst();
if (res[index] == nums[index]) {
dq.removeFirst();
}
}
return res;
}
4 总结
三种方法解决,如何解决堆储存重复元素的问题。
leetcode面试准备:Sliding Window Maximum的更多相关文章
- 【LeetCode】239. Sliding Window Maximum
Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving fr ...
- 【刷题-LeetCode】239. Sliding Window Maximum
Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving from ...
- 【LeetCode】239. Sliding Window Maximum 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减队列 MultiSet 日期 题目地址:ht ...
- 【LeetCode 239】Sliding Window Maximum
Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...
- [LeetCode] Sliding Window Maximum 滑动窗口最大值
Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...
- Leetcode: sliding window maximum
August 7, 2015 周日玩这个算法, 看到Javascript Array模拟Deque, 非常喜欢, 想用C#数组也模拟; 看有什么新的经历. 试了四五种方法, 花时间研究C# Sorte ...
- [LeetCode] 239. Sliding Window Maximum 滑动窗口最大值
Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...
- LeetCode题解-----Sliding Window Maximum
题目描述: Given an array nums, there is a sliding window of size k which is moving from the very left of ...
- [leetcode]239. Sliding Window Maximum滑动窗口最大值
Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...
随机推荐
- 【JAVA】final修饰Field
一.简介 final修饰符可以用来修饰变量.方法.类.final修饰变量时一旦被赋值就不可以改变. 二.final成员变量 成员变量是随类初始化或对象初始化而初始化的.当类初始化的时候,会给类变量分配 ...
- ubuntu修改grub背景
grub背景由/etc/grub.d/05_debian_theme定义,修改图片只需要将图片文件放到/boot/grub,d/下即可, 修改颜色只需编辑/boot/grub.d/grub.cfg
- Swift 2.0基本语法
内容包括:01变量&常量 02分支 03循环 04字符串 05数组 06字典 07函数 01变量&常量 //: Playground - noun: a place where peo ...
- 用python下载辞典
用python下载词源词典Etymoline Online Etymology Dictionary是最好的 English 词源词典,现在来说没有之一.但是,一直在PC上查单词有时不是很方便,遂就想 ...
- STL简介
由于不同书籍和翻译问题对STL中的术语可能有差别本文采用侯杰<STL源码剖析>中的术语 STL的组件 包含6个组件,分别为容器.算法.迭代器.仿函数(函数对象).配接器(适配器).配置器( ...
- Ubuntu 14.04为浏览器添加Flash插件
在刚安装好到Ubuntu操作系统中默认是没有flash支持到,因此,当我们使用浏览器查看很多视频网页到时候,会导致网页上到视频无法播放.然而,这个问题我们也不能够通过“软件中心”来解决,这时候需要我们 ...
- Web前端新人笔记之height、min-height的区别
浏览器参照基准:Firefox, Chrome, Safari, Opera, IE: * IE6不支持CSS min-height属性.最小高度的定义:1. 元素拥有默认高度:2. 当内容超出元素 ...
- PS初始化配置
前端工程师在使用photoshop之前需要进行一些初始化设置,主要包括以下3个 [1]首选项设置 <ctrl+k> 编辑 > 首选项 > 单位与标尺 > 把标尺和文字的单 ...
- sae-php调试代码,不输出页面
如果单单使用sae_debug,页面就会输出SAE_DEBUG的信息,所以造成很多问题,例如回复微信服务器的xml,但是不知道就想着调试,结果... 所以怎么解决让调试信息不输出页面呢 看完手册,才知 ...
- 树莓派 raspberry 入门之安装操作系统以及配置
最近新入手一树莓派,型号是2代B,屏幕是微雪的7 inch c型 显示屏.下面来教大家怎么点亮树莓派. 第一步,装好显示器,显示器的电源接在树莓派的usb口上,HDMI口不多说,连上.然后装好鼠标.键 ...