(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, ...
随机推荐
- Raid1源代码分析--读流程
这篇博文不足之处较多,重新整理了一下,链接:http://www.cnblogs.com/fangpei/p/3890873.html 我阅读的代码的linux内核版本是2.6.32.61.刚进实验室 ...
- cocos2dx-lua绑定自定义c++类(二)
在 cocos2dx-lua绑定自定义c++类(一)中介绍了如何产生绑定文件. 现在,来看看怎么在工程里使用这个cpp文件.像普通源码文件一样,导入到工程里,看到 LuaTest.h文件里有一个函数入 ...
- 一、mysql分表简单介绍
一.Mysql分表的原因 1.当一张的数据达到几百万时,你查询一次所花的时间会变多,如果有联合查询的话,我想有可能会死在那儿了. 分表的目的就在于此,减小数据库的负担,缩短查询时间. 2.mysql中 ...
- Linux一个简单的读写文件
(1)linux中的文件描述符fd的合法范围是或者一个正正数,不可能是一个负数. (2)open返回的fd程序必须记录好,以后向这个文件的所有操作都要靠这个fd去对应这个文件,最后关闭文件时也需要fd ...
- 单片微机原理P4:80C51串口与串行总线拓展
0. 串口通讯 0. 串口通讯的数据传输方式:单工(单向传输数据),半双工(非同时双向传输),全双工(同时,双向传输) 1. 根据通信方式的不同又分为同步通讯和异步通讯. 同步通讯:所有设备都使用同一 ...
- maven编写主代码与测试代码
3.2 编写主代码 项目主代码和测试代码不同,项目的主代码会被打包到最终的构件中(比如jar),而测试代码只在运行测试时用到,不会被打包.默认情况下,Maven假设项目主代码位于src/main/ja ...
- swift 弹窗
直接拷贝使用即可 let alert = UIAlertController(title:nil,message:"输入不能为空",preferredStyle: .Alert) ...
- for的用法
第一次看到这么用,哈哈,就记下 for (var control = ["程", "陈", "是"]; control[0]; contro ...
- [Angular 2] WebStorm - Managing Imports
Some tips for import libaray by using webstorm: // Alt + Enter --> Auto Import // Ctrl + Alt + o ...
- 并行计算基础&编程模型与工具
在当前计算机应用中,对快速并行计算的需求是广泛的,归纳起来,主要有三种类型的应用需求: 计算密集(Computer-Intensive)型应用,如大型科学project计算与数值模拟: 数据密集(Da ...