【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 ...
随机推荐
- Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏
原文:Codelab for Android Design Support Library used in I/O Rewind Bangkok session--Make your app fanc ...
- Jenkins 构建JavaHelloWorld
原文:http://www.cnblogs.com/itech/archive/2011/11/03/2234662.html 注意:我们知道Jenkins通过master/slave来支持分布式的j ...
- Data Flow ->> Union All
Wrox的<Professional Microsoft SQL Server 2012 Integration Services>一书中再讲Merge的时候有这样一段解释: This t ...
- Angular service
<!DOCTYPE html> <html lang="en" ng-app="myApp"> <head> <met ...
- SQL Server 联表字段合并查询
经常遇到统计报表中,子表记录合并为一个字段的情况.例如:省表中各省经济水平前五的城市统计. 有如下两表:dbo.省 和 dbo.市 (好吧,你可能会吐槽为什么用中文表名,其实我是为了方便查找替换) 这 ...
- Java实体书写规范
** * 用户角色表 */ public class BaseUserRole implements Serializable { private static final long serialVe ...
- Oracle中用一个表的数据更新另一个表的数据
update tbl1 a set (a.col1, a.col2) = (select b.col1, b.col2 from tbl2 ...
- CString和string
CString和string(一) 概述 string和CString均是字符串模板类,string为标准模板类(STL)定义的字符串类,已经纳入C++标准之中: CString(typedef CS ...
- STL笔记(6)标准库:标准库中的排序算法
STL笔记(6)标准库:标准库中的排序算法 标准库:标准库中的排序算法The Standard Librarian: Sorting in the Standard Library Matthew A ...
- hdu4422The Little Girl who Picks Mushrooms
4422 小于等于3 的时候就是1024 4的时候 讨论 5的时候讨论 注意重量为0的情况 #include <iostream> #include<cstdio> #incl ...