【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 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
--------------- -----
[ -] -
[ - -]
[- - ]
- [- ]
- - [ ]
- - [ ]
Therefore, return the max sliding window as [3,3,5,5,6,7].
题意:
给定一个数组序列,以及一个固定大小的窗口K,用k从前到后扫描一次数组,返回窗口每个状态下的最大值。
思路:
使用双端队列deque当做滑动窗口,保证每个状态下最大值总是在队首。比如[1,3,-1,-3,5,3,6,7],那么deque的状态分别为[1] 、[3] (前2步,填装滑动窗口) ,(从第三步正式开始)[3,-1]、 [3,-1,-3]、[5]、[5,3]、[6]、[7],可以看到每次队列的队首元素都是当前滑动窗口的最大值,具体过程可参考代码,有注释。时间复杂度O(n)。
C++:
class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
vector<int> ret;
if(nums.size() == )
return ret;
//双端队列,存储的是元素的下标,不是元素的值
deque<int> slideWindow;
for(int i = ; i < k; i++)
{
//如果要进队列的数比它前面的数字大,则将它前面的数字从后面删除,直到它前边的数字比它大或者队列为空
while(!slideWindow.empty() && nums[i] >= nums[slideWindow.back()])
{
slideWindow.pop_back();
}
//加入新元素到队列
slideWindow.push_back(i);
}
for(int i = k; i < nums.size(); i++)
{
//队首元素即为当前窗口最大值
ret.push_back(nums[slideWindow.front()]);
//如果要进队列的数比它前面的数字大,则将它前面的数字从后面删除,直到它前边的数字比它大或者队列为空
while(!slideWindow.empty() && nums[i] >= nums[slideWindow.back()])
{
slideWindow.pop_back();
}
//如果当前队首的元素已经不在窗口内部,则将其从队列前边删除
if(!slideWindow.empty() && slideWindow.front() <= i - k)
slideWindow.pop_front();
//加入新元素到队列
slideWindow.push_back(i);
}
//不要忘了最后一个窗口的结果 - -
ret.push_back(nums[slideWindow.front()]);
return ret;
}
};
【LeetCode 239】Sliding Window Maximum的更多相关文章
- leetcode面试准备:Sliding Window Maximum
leetcode面试准备:Sliding Window Maximum 1 题目 Given an array nums, there is a sliding window of size k wh ...
- 【POJ 2823】【Luogu P1886】Sliding Window 滑动窗口
POJ 2823 Luogu P1886 [解题思路] 这是一个单调队列算法的经典题目,几乎学习单调队列的人都接触过这题. 利用单调队列算法求出每一个固定区间内的最(大/小)值. 以下以最大值为例: ...
- 【原创】Sliding Window Maximum 解法分析
这道题是lintcode上的一道题,当然leetcode上同样有. 本题需要寻找O(N)复杂度的算法. 解体思路比较有特点,所以容易想到参考 最小栈 的解题办法. 但是最小栈用栈维护最小值很直观,这道 ...
- 【LeetCode OJ】Binary Tree Maximum Path Sum
Problem Link: http://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ For any path P in a bina ...
- 【LeetCode练习题】Minimum Window Substring
找出包含子串的最小窗口 Given a string S and a string T, find the minimum window in S which will contain all the ...
- 【leetcode❤python】 414. Third Maximum Number
#-*- coding: UTF-8 -*- #l1 = ['1','3','2','3','2','1','1']#l2 = sorted(sorted(set(l1),key=l1.index,r ...
- 【POJ 2823】Sliding Window(单调队列/堆)
BUPT2017 wintertraining(16) #5 D POJ - 2823 题意 给定n,k,求滑窗[i,i+k-1]在(1<=i<=n)的最大值最小值. 题解 单调队列或堆. ...
- 【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 ...
随机推荐
- scala函数式编程
1.作为值的函数 在Scala中,函数和数字一样,可以在变量中存放函数.可以把函数赋值给一个变量,格式为:val foee=fun _(函数名+空格+_)形式 2.匿名函数 在scala中,不需要给每 ...
- 脉络清晰的BP神经网络讲解,赞
学习是神经网络一种最重要也最令人注目的特点.在神经网络的发展进程中,学习算法的研究有着十分重要的地位.目前,人们所提出的神经网络模型都是和学习算 法相应的.所以,有时人们并不去祈求对模型和算法进行严格 ...
- php静态
static 是定义一个静态对象或静态变量,关于static 定义的变量或类方法有什么特性我们看完本文章的相关实例后就见分晓了. 1. 创建对象$object = new Class(),然后使用”- ...
- sql 随笔 2015-08-07
xls 导入数据库 --删除现有数据 DELETE FROM dbo.PhoneList --插入数据 insert into dbo.PhoneList --读取xls数据 ) , as [Enab ...
- GitHub 教程 in Ubuntu
Follow these steps to configure github if you are the first time to use Github 1. Sign up a username ...
- C++ const && 二叉树合集
话说昨天因为校园网的问题导致现在才发博文~唉,想吐槽~ 这个是昨天写的,觉得,用来回顾还是很不错的,比较具体的都在笔记中,尤其我觉得里面经验性的东西还是不错的. 2013-8-26 今天在回顾我以前写 ...
- hdu 3177
题目大意:向体积为v的山洞中搬运n个物品,每个物品具有(a,b) 属性.其中a是停放体积,b是移动体积.输出这个山东是否能放下这n个物品 解题思路: 1)当前物品能否放进山洞取决于当前物品的的移动体积 ...
- Asterisk的配置详解
Asterisk的配置文件都在/etc/asterisk目录下,重要的配置文件有: sip.conf sip电话基本配置 extensions.conf ...
- allegro下快捷键设置[转贴]
zz : http://yuandi6.blog.163.com/blog/static/207265185201210245435397/ 修改变量文件,设置自定义快捷键. Allegro可以通过修 ...
- Idea 使用maven+tomcat的时候,编译指定的Profile
To build a artifact with a profile you have to create a Maven Run/Debug configuration as in the foll ...