(heap)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 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.
Follow up:
Could you solve it in linear time?
解:
维护一个双端队列保存可能成为当前窗口最大值的所有候选值的索引,队列中的索引表示的值递减,即队列第一个元素为队列中最大值
对于题目给的例子:
首先进队数组中第一个元素1的索引0,当到达第二个值3时,出队索引0,因为其代表的值1不可能成为窗口中的最大值(因为比3小),
到达第三个值-1,比当前队列中索引代表的值3小,必须进队,因为等下如果3从左侧滑出窗口,-1可能成为窗口的最大值。
dq: (1) 3 -1 -3 5(先出队3(超出窗口),接着出队队尾的-1,-3(小于5))
Java代码:
public class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
if(nums == null || nums.length == 0)
return nums;
int len = nums.length;
int[] result = new int[len-k+1]; //结果数组
int ri = 0;
//存储递减值的索引的队列
Deque<Integer> dq = new ArrayDeque<Integer>();
for(int i=0; i<len; i++){
//出队超出窗口的索引
if(!dq.isEmpty() && dq.peek() < i-k+1)
dq.poll();
//当窗口末尾即队列尾的数比当前滑进的数小,则出队
while(!dq.isEmpty() && nums[dq.peekLast()] < nums[i])
dq.pollLast();
dq.offer(i);
//窗口满数时,把最大值加入结果数组
if(i >= k-1)
result[ri++] = nums[dq.peek()];
}
return result;
}
}
Python代码:
class Solution:
# @param {integer[]} nums
# @param {integer} k
# @return {integer[]}
def maxSlidingWindow(self, nums, k):
d = collections.deque()
re = []
for i, v in enumerate(nums):
while d and nums[d[-1]] < v:
d.pop()
d.append(i)
if d[0] == i-k:
d.popleft()
if i >= k-1:
re.append(nums[d[0]])
return re
(heap)239. 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 ...
- 239. Sliding Window Maximum *HARD* -- 滑动窗口的最大值
Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...
- 239. Sliding Window Maximum
题目: Given an array nums, there is a sliding window of size k which is moving from the very left of t ...
- [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] 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】239. Sliding Window Maximum 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减队列 MultiSet 日期 题目地址:ht ...
- leetcode 239 Sliding Window Maximum
这题是典型的堆排序算法,只是比一般的堆算法多了删除的操作,有两件事需要做: 1 用一个hash表存储从输入数组索引到堆数组(用于实现堆的那个数组)所以的映射,以便在需要删除一个元素的时候能迅速定位到堆 ...
- 239 Sliding Window Maximum 滑动窗口最大值
给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口 k 内的数字.滑动窗口每次只向右移动一位.例如,给定 nums = [1,3,-1,-3, ...
随机推荐
- SRM 599 DIV1
A 首先发现对于2操作,每种素因子可以单独考虑,然后取出步数最多的计入答案,然后分别加上对每种素因子的1操作; 第二步我犯了个错误,以为最优方案是把素因子指数按二进制操作,在1的位置执行1操作,0的位 ...
- Android——自定义Actionbar左侧覆盖不全的解决方案
今天遇到一个很蛋疼的问题,就是在自定义Actionbar的时候,setCustomView中,自定义的view怎么也覆盖不了整个视图,左侧一直留有一个空白,看下图: 所写的部分代码如下: protec ...
- WPF与输入法冲突研究之一:百度输入法会导致WPF程序的崩溃!
在学习和使用了WPF一段时间之后,有点感觉WPF是个不太成熟的框架,不知道是我学的太肤浅,还是WPF得BUG太多! >>>>>>>模拟场景<<&l ...
- C#使用WebKitBrowser.dll填坑记
.Net 自带的 Webbrowser 有着太多的平台限制.对于用户体验之上的今天,这无疑是一个噩梦, 然后就开始找 .Net下的WebKitBrowser.dll (后面提供下载) 从开源网站下到程 ...
- [Angular 2] Using the @Inject decorator
TypeScript is used heavily as we build up our application, but TypeScript isn’t required. If you wan ...
- 程序员的绘图利器 — Gnuplot
介绍 Gnuplot is a command-line program that can generate two- and three-dimensional plots. It is fre ...
- VS2010不能打开预编译的网站源码的原因是什么?(转之csdn)
原问题: 今天将写好的一个网站源码目录拷贝到另一台电脑上,但打开时提示: 你要打开一个预编译的网站,你可以查看该站点,但对它进行更改可能会造成该网站停止运行,若要修改站点,建议先编辑原始网站中的 ...
- Jquery:Jquery中的DOM操作<二>
由于昨天晚上回来的晚,写的有点匆忙,所以昨天的学习笔记中出现了多处错误的地方,幸好有各位园友帮忙指出,在这里谢过各位了!今天继续学习关于Jquery中DOM的操作,其实是昨天随笔的延续,求围观!!! ...
- IIS报500.0错误
IIS安全里面配置:Everyone.IUSR.IIS_IUSRS 参考地址:http://blog.chinaunix.net/uid-21375345-id-3213631.html
- WCF入门教程系列四
一.概述 配置也是WCF编程中的主要组成部分.在 以往的.net应用程序中,我们会把DBConn和一些动态加载类及变量写在配置文件里.但WCF有所不同.他指定向客户端公开的服务,包括服务的地址. 服务 ...