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

Therefore, return the max sliding window as [3,3,5,5,6,7].

Note: 
You may assume k is always valid, ie: 1 ≤ k ≤ input array's size for non-empty array.

Follow up:
Could you solve it in linear time?

Hint:

  1. How about using a data structure such as deque (double-ended queue)?
  2. The queue size need not be the same as the window’s size.
  3. Remove redundant elements and the queue should store only elements that need to be considered.
 
参考fentoyal的单调队列做法,我加上一些解释。
双端队列mq存放的是二元组,
第一元素表示当前值,
第二元素表示在队列中末尾值之后,在当前值之前并且小于当前值的个数。
mq的首个二元组的第一元素表示窗口范围内的最大值。
max函数:
  返回首个二元组的第一元素
push函数:
  为了保证max函数的作用,需要从末尾开始,把小于当前值的连续二元组出队列,但是需要累加上小于当前值的数目。
pop函数:
  如果窗口在首个二元组之前还有元素,那么对队列影响就是首个二元组的计数减一。
  否则代表首个二元组就是待出队的值,出队。
 
class MonoQue
{
public:
deque<pair<int, int> > q;
int maxV()
{
return q.front().first;
}
void push(int n)
{
int count = ;
while(!q.empty() && q.back().first < n)
{
count += (q.back().second + );
q.pop_back();
}
q.push_back(make_pair(n, count));
}
void pop()
{
if(q.front().second > )
q.front().second --;
else
q.pop_front();
}
}; class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
vector<int> ret;
if(k == )
return ret;
MonoQue mq;
for(int i = ; i < k; i ++)
mq.push(nums[i]);
for(int i = k; i < nums.size(); i ++)
{
ret.push_back(mq.maxV());
mq.pop();
mq.push(nums[i]);
}
ret.push_back(mq.maxV());
return ret;
}
};

【LeetCode】239. Sliding Window Maximum的更多相关文章

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

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

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

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

  3. 【原创】leetCodeOj --- Sliding Window Maximum 解题报告

    天,这题我已经没有底气高呼“水”了... 题目的地址: https://leetcode.com/problems/sliding-window-maximum/ 题目内容: Given an arr ...

  4. 【leetcode】239. 滑动窗口最大值

    目录 题目 题解 三种解法 "单调队列"解法 新增.获取最大值 删除 代码 题目 给你一个整数数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以 ...

  5. leetcode面试准备:Sliding Window Maximum

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

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

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

  8. 239. Sliding Window Maximum

    题目: Given an array nums, there is a sliding window of size k which is moving from the very left of t ...

  9. 239. Sliding Window Maximum *HARD* -- 滑动窗口的最大值

    Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...

随机推荐

  1. Codeforces Round #341 (Div. 2) E - Wet Shark and Blocks

    题目大意:有m (m<=1e9) 个相同的块,每个块里边有n个数,每个数的范围是1-9,从每个块里边取出来一个数组成一个数,让你求组成的方案中 被x取模后,值为k的方案数.(1<=k< ...

  2. BZOJ1192 [HNOI2006]鬼谷子的钱袋 数学推理

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1192 题意概括 把一个数m拆成很多数字. 问至少拆成多少个数字,1~m中的所有数字才可以用这些数字 ...

  3. vi中批量加注释

    用v进入virtual模式 按Control+v(win下面ctrl+q)进入列模式 上下键来进行选择 I进行输入(shift+i) 按两次ese键

  4. Repository HDU2846

    极限过的 最原始的方法一层一层建树就好了 #include<bits/stdc++.h> using namespace std; ][]={}; ]={}; ]; ; int pos; ...

  5. 001 Python中的基本类型初步介绍

    这个但是根据书来整理的,显得有些多,也不够完整. 一:介绍 1.为什么使用内置对象 对象类型是语言的一个部分 内置对象构成了每个python程序的核心部分 二:数字 1.**是乘方 2.math数学模 ...

  6. 《Gradle权威指南》--Android Gradle NDK支持

    No1: 在根项目下的local.properties文件中配置 sdk.dir=/home/frame/android/android-sdk ndk.dir=/home/frame/android ...

  7. 如何在VS Code中进行golang编程

    在VS Code中编程 使用Visual Studio Code的Go扩展,您可以获得诸如IntelliSense,代码导航,符号搜索,括号匹配,片段等语言功能,这些功能将帮助您进行Golang开发. ...

  8. hdu1698 Just a Hook 【区间修改】(模板题)

    题目链接:https://vjudge.net/contest/182746#problem/E 题目大意: 一段线段由n条小线段组成,每次操作把一个区间的小线段变成金银铜之一(金的价值为3,银为2, ...

  9. 015.Linux系统删根数据恢复

    重要声明: 本文档所有思路来自于誉天教育,由本人(木二)在实验环境验证通过: 您应当通过誉天邹老师或本人提供的其他授权通道下载.获取本文档,且仅能用于自身的合法合规的业务活动: 本文档的内容仅供学习交 ...

  10. Android二维码学习

    http://www.cnblogs.com/liuan/category/347622.html http://blog.csdn.net/xiaanming/article/details/101 ...