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. ArcMap中用python的split方法提取字段的值

    提取PROPERTY_L字段空格分隔符前面的地址编号 提取前:5105 ABERDEEN LANE 提取后:5105 提取的表达式:!PROPERTY_L!.split(" ")[ ...

  2. 通过代码退出IOS程序

    -(void) tapClick:(UITapGestureRecognizer *)tap{ [UIViewbeginAnimations:@"exitApplication"c ...

  3. react 第一个组件 “hello world!”

    一:在src下面新建Welcome.js 二:在Welcome.js中使用类式写法: import React from "react" class Welcome extends ...

  4. DB数据源之SpringBoot+Mybatis踏坑过程实录系列(一)

    DB数据源之SpringBoot+MyBatis踏坑过程(一) liuyuhang原创,未经允许进制转载 系列目录 DB数据源之SpringBoot+Mybatis踏坑过程实录(一) DB数据源之Sp ...

  5. Qt5显示中文字符

    在cpp文件或.h文件中顶行输入: #pragma execution_character_set("utf-8")

  6. python新建一个表格xls并写入数据

    # -*- coding:utf-8 -*- import xlwt workbook = xlwt.Workbook() # 新建一个工作簿 sheet = workbook.add_sheet(& ...

  7. sign

    sign字段构成:登录类型(2Bytes) + userid(不定长,最长10Bytes,用户id或设备id) + time(10Bytes) + token(32Bytes).其中:token = ...

  8. Home Assistant系列 -- 设置界面语言与地理位置

    Home Assistant 安装的时候会自动根据你的系统语言设置默认语言,安装完成以后也可以根据需要自己设置选择语言.启动 Home Assistant ,浏览器打开web 界面,点击左上角的用户图 ...

  9. Django模型定义参考

    字段 对字段名称的限制 字段名不能是Python的保留字,否则会导致语法错误 字段名不能有多个连续下划线,否则影响ORM查询操作 Django模型字段类 字段类 说明 AutoField 自增ID字段 ...

  10. MFC中两个对话框之间数据传递

    以下是在网上参考的一篇文章,网址:https://blog.csdn.net/foreverhuylee/article/details/21707197 这里有两种情况, 第一种情况是: (在一个基 ...