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. A 暴力搜索 剪枝是关键

    Description 盖伦是个小学一年级的学生,在一次数学课的时候,老师给他们出了一个难题:老师给了一个正整数 n,需要在不大于n的范围内选择三个正整数(可以是相同的),使它们三个的最小公倍数尽可能 ...

  2. LeetCode 4. Median of Two Sorted Arrays (分治)

    两个有序的数组 nums1 和 nums2 维数分别为m,n.找所有数的中位数,复杂度 O(log (m+n)) 注意:奇偶个数,分治法求解,递归出口特殊处理.取Kth smallest数时,分治取m ...

  3. 【BZOJ1786】[Ahoi2008]Pair 配对

    题解: 打表出奇迹 能发现所有ai一定是不减的 其实很好证明啊.. 考虑两个位置x y(y在x右边) x的最优值已经知道了 考虑y处 先让y=x,然后开始变化 因为x处已经是最优的了,所以如果减小,那 ...

  4. 多线程中使用CheckForIllegalCrossThreadCalls = false访问窗口

    在多线程程序中,新创建的线程不能访问UI线程创建的窗口控件,如果需要访问窗口中的控件,可以在窗口构造函数中将CheckForIllegalCrossThreadCalls设置为 false publi ...

  5. 【Java】 剑指offer(57-1) 和为s的两个数字

      本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 输入一个递增排序的数组和一个数字s,在数组中查找两个数,使得它 ...

  6. HDU-2087-剪花布条 【KMP】(求模式串的匹配个数——与已匹配的字串不交)

    题目链接:https://vjudge.net/contest/220679#problem/C 剪花布条                                               ...

  7. 基于 Jenkins+Docker+Git 的CI流程初探

    在如今的互联网时代,随着软件开发复杂度的不断提高,软件开发和发布管理也越来越重要.目前已经形成一套标准的流程,最重要的组成部分就是持续集成(Continuous Integration,CI)及持续部 ...

  8. IntelliJ IDEA关于logger的live template配置

    1.安装 log support2插件 2.配置log support2 由于项目中的日志框架是公司自己封装的,所以还需要自己手动改一下 log support2插件生成的live template ...

  9. 利用python计算多边形面积

    最近业务上有一个需求,给出多边形面积. Google了一下,发现国内论坛给的算法都是你抄我我抄你,也不验证一下是否正确, 从 博客园到csdncsdn 然后传播到国内各个角落...真是无力吐槽了. 直 ...

  10. win10 共享 及 媒体流

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha