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. 使用supervior 监控 elasticsearch 进程

    elasticsearch引擎在使用中可能会出现后台守护进程挂掉的情况,需要手动启动来恢复正常. 这时则可以引用supervior进程管理工具来监控elasticsearch进程状态,实现进程挂掉自动 ...

  2. 解决apache启动错误:Could not reliably determine the server's fully qualified domain name

    启动apache遇到错误:httpd: Could not reliably determine the server's fully qualified domain name [root@serv ...

  3. 黑少微服务商店之Iron Cloud微服务开发云

    近日,由黑少微服务研发团队推出的Iron Cloud微服务开发云已经正式对外提供服务,这是国内第一家基于云端操作的微服务专业开发工具. Iron Cloud 微服务开发云(www.ironz.com) ...

  4. Web—13-判断网站请求来自手机还是pc浏览器

    判断网站请求来自手机还是pc浏览器 #判断网站来自mobile还是pc def checkMobile(request): """ demo : @app.route(' ...

  5. CodePush热更新组件详细接入教程

    CodePush热更新组件详细接入教程 什么是CodePush CodePush是一个微软开发的云服务器.通过它,开发者可以直接在用户的设备上部署手机应用更新.CodePush相当于一个中心仓库,开发 ...

  6. iframe空白

    优酷网页上复制的通用代码用来播放视频,浏览器和谷歌模拟器正常,但是发布后手机上打开一片空白,一直以为是苹果手机不支持iframe,最后发现是由于iframe播放链接是http的,而我们网页是https ...

  7. 3x3开窗中值滤波器的FPGA硬件实现

    数字逻辑课程的自由设计中,我编写了一个3x3开窗的中值滤波器,处理一副128*128像素值的图像,并且最终可以在FPGA上板实现. 中值滤波的本质就是对于一个n*n的窗口,将其内部的值进行排序,取中位 ...

  8. Linux入门第二天——基本命令入门(下)

    一.帮助命令 1.帮助命令:man (是manual手册的缩写,男人无所不能,/笑哭) 更多man用法以及man page的用法,参见:http://www.linuxidc.com/Linux/20 ...

  9. 20155204 2016-2017-2 《Java程序设计》第10周学习总结

    20155204 2016-2017-2 <Java程序设计>第10周学习总结 教材学习内容总结 1.计算机网络概述 路由器和交换机组成了核心的计算机网络,计算机只是这个网络上的节点以及控 ...

  10. 20155236 《Java程序设计》实验三(敏捷开发与XP实践)实验报告

    20155236 <Java程序设计>实验三(敏捷开发与XP实践)实验报告 一.实验内容及步骤 XP基础 XP核心实践 相关工具 实验内容 1.在IDEA中使用工具(Code->Re ...