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. Return the max sliding window.

Example:

Input: nums = [1,3,-1,-3,5,3,6,7], and k = 3
Output: [3,3,5,5,6,7]
Explanation:

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



#include <cstdio>
#include <vector>
#include<deque>
#include<iostream>
using namespace std; class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
deque<int> buffer;
vector<int> res; for(int i=; i<nums.size();++i)
{
while(!buffer.empty() && nums[i]>=nums[buffer.back()]) buffer.pop_back();
buffer.push_back(i); if(i>=k-) res.push_back(nums[buffer.front()]);
if(buffer.front()<= i-k + ) buffer.pop_front();
}
return res;
}
};
int main(){
Solution test;
vector<int> aa;
aa.push_back();
aa.push_back(-);
aa.push_back();
aa.push_back();
aa.push_back();
aa.push_back();
aa.push_back();
aa.push_back();
test.maxSlidingWindow(aa,);
return ;
}

239. [LeetCode ]Sliding Window Maximum的更多相关文章

  1. 【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 ...

  2. [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 ...

  3. Leetcode: sliding window maximum

    August 7, 2015 周日玩这个算法, 看到Javascript Array模拟Deque, 非常喜欢, 想用C#数组也模拟; 看有什么新的经历. 试了四五种方法, 花时间研究C# Sorte ...

  4. 【LeetCode】239. Sliding Window Maximum

    Sliding Window Maximum   Given an array nums, there is a sliding window of size k which is moving fr ...

  5. 【刷题-LeetCode】239. Sliding Window Maximum

    Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving from ...

  6. leetcode面试准备:Sliding Window Maximum

    leetcode面试准备:Sliding Window Maximum 1 题目 Given an array nums, there is a sliding window of size k wh ...

  7. [LeetCode] Sliding Window Median 滑动窗口中位数

    Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...

  8. [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 ...

  9. 【LeetCode】239. Sliding Window Maximum 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减队列 MultiSet 日期 题目地址:ht ...

随机推荐

  1. 2017年秋招美团Java程序员开发,看我如何拿到offer

    本人是一名本科毕业非计算机专业的程序员,面了阿里,结果没过,最后面上了美团,热乎乎的面经,昨天面的美团,虽然面完了HR面,但是感觉希望不大,希望能走运拿到offer吧.记性不是太好,有一些问题没能记住 ...

  2. 用 map 表达互斥逻辑

    在这个开发周期遇到这样一个需求: 管理员可以给子账号配置权限,有些权限存在互斥不可同时勾选,比如 审核员和代采.审核和采购员不可同时勾选 之前同事实现的方式如下: 这样每添加一个互斥关系就要遍历一次, ...

  3. Mac 模拟慢速网络

    作为开发者,为了提升用户体验,有时需要模拟不同环境的网络.Mac环境下模拟慢速网络可以使用苹果官方提供的工具:Network Link Conditioner. 1.点击苹果开发者网站提供的下载页面, ...

  4. Python绘制奥运五环

    绘制奥运五环主要涉及到Python中的turtle绘图库运用: turtle.forward(distance) 向当前画笔方向移动distance像素长度 turtle.backward(dista ...

  5. Centos6.5中如何用sqlite3命令打开’.db’后缀的数据库执行sql

      1. 简单sql语句使用: 在任意目录下新建一个数据库,比如student . 命令: sqlite3 student.db 出现如下提示: 输入sql语句create table user(us ...

  6. Some cool FireMonkey multi-device components

    http://blogs.embarcadero.com/davidi/2014/01/16/43281 There are many available Delphi and C++Builder ...

  7. 1.Hadoop集群安装部署

    Hadoop集群安装部署 1.介绍 (1)架构模型 (2)使用工具 VMWARE cenos7 Xshell Xftp jdk-8u91-linux-x64.rpm hadoop-2.7.3.tar. ...

  8. 数据立方体(Cube)

    如上图所示,这是由三个维度构成的一个OLAP立方体,立方体中包含了满足条件的cell(子立方块)值,这些cell里面包含了要分析的数据,称之为度量值.显而易见,一组三维坐标唯一确定了一个子立方. 多位 ...

  9. Windows系统Python 虚拟环境virtualenv安装

    1.我们用pip安装virtualenv >pip3 install virtualenv 2.创建工程目录 >mkdir myproject 3.进入工程目录 >cd myproj ...

  10. 字符编码——python学习

    python学习—字符编码 例如汉字“中” 十进制:20013 二进制:01001110 00101101(unicode)/11100100 10111000 10101101(utf-8) 十六进 ...