239. [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 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的更多相关文章
- 【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 ...
- [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 ...
- Leetcode: sliding window maximum
August 7, 2015 周日玩这个算法, 看到Javascript Array模拟Deque, 非常喜欢, 想用C#数组也模拟; 看有什么新的经历. 试了四五种方法, 花时间研究C# Sorte ...
- 【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 ...
- leetcode面试准备:Sliding Window Maximum
leetcode面试准备:Sliding Window Maximum 1 题目 Given an array nums, there is a sliding window of size k wh ...
- [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 ...
- [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 ...
- 【LeetCode】239. Sliding Window Maximum 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减队列 MultiSet 日期 题目地址:ht ...
随机推荐
- 创建 个人 pod
创建一个自己的 pod 大致需要以下步骤 创建git repository 编辑.podspec 创建LICENSE(许可证/授权)文件 标记 tag 验证 注册CocoaPods 发布 搜索验证 1 ...
- iOS 百度地图判断用户是否拖动地图的检测方法
前言:百度地图API并没有提供移动地图时的回调接口 实现:通过判断当前地图的中心位置是否为用户位置来判断,代码如下 -(void)mapView:(BMKMapView *)mapView regio ...
- C# 反射 Reflection Assembly
本章节带来的是反射,反射反射程序员的快乐. 一.什么叫反射 反射:是.net Framework提供给的一个方面metadata的帮助类,可以获取信息并且使用 反射的有点:动态 反射的缺点:1:稍微麻 ...
- CF451E Devu and Flowers(组合数)
题目描述 Devu想用花去装饰他的花园,他已经购买了n个箱子,第i个箱子有fi朵花,在同一个的箱子里的所有花是同种颜色的(所以它们没有任何其他特征).另外,不存在两个箱子中的花是相同颜色的. 现在De ...
- 水仙花数(类型:一级、C++)
题目描述: 输入一个三位数n,判断是否为水仙花数,如果是则输出“YES”,不是则输出“NO”.水仙花数:是指一个3位数,它的每个位上的数字的3次幂之和等于它本身.(例如:1^3 + 5^3+ 3^3 ...
- Struts2速记手册
工作原理 Action类 Action类 普通Action类 私有属性及getter.setter(处理请求参数) execute()方法(处理请求) 实现Action接口 提供常量 继承Ac ...
- MongoDB数据库 : 基础
三元素:数据库 集合 文档(json的扩展bson) 服务启动重启停止: sudo service mongodb start(stop,restart) 修改配置文件 /etc/mongodb.co ...
- Zeta--S3 Linux使用PCCAM/WEBCAM模式
#include <ZetaCameraInterface.h> #include <ZetaMediaPlayInterface.h> using namespace zet ...
- ACM1013:Digital Roots
Problem Description The digital root of a positive integer is found by summing the digits of the int ...
- node.js之express中app.use
express中app.use 用法: app.use([path,] function [, function…]) 一.app.use() 在express中是怎么工作的 app.use在expr ...