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 knumbers 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.
class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
deque<int> dq;
vector<int> ans;
int n = nums.size(), i;
for (i = ; i < n; i++)
{
if (dq.front() == i - k)
dq.pop_front();
while (!dq.empty() && nums[dq.back()] < nums[i])
dq.pop_back();
dq.push_back(i);
if (i >= k - )
ans.push_back(nums[dq.front()]);
}
return ans;
}
};

双端队列

239. Sliding Window Maximum *HARD* -- 滑动窗口的最大值的更多相关文章

  1. 【LeetCode】239. Sliding Window Maximum

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

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

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

  5. 239 Sliding Window Maximum 滑动窗口最大值

    给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口 k 内的数字.滑动窗口每次只向右移动一位.例如,给定 nums = [1,3,-1,-3, ...

  6. POJ 2823 Sliding Window & Luogu P1886 滑动窗口

    Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 66613   Accepted: 18914 ...

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

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

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

  9. (heap)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 ...

随机推荐

  1. 【转载】免费台北.edu教育邮箱及Office 365 Education申请

    免费的邮箱非常多,但是免费的.edu教育邮箱却很少有.记得上次不少人寻找.edu教育邮箱还是因为国外一家VPS商家推出的专门针对学生的优惠包,使用.edu教育邮箱就可以获得50美元的优惠,真的很划算. ...

  2. js高级程序设计书中,有一句话在全局作用域中定义的函数实际上只 能被某个对象调用???

    js没有块级作用域(题外话:函数可以作为一个块级),所以我们经常使用闭包来模拟块级作用域,以避免变量或者函数因为名称相同而产生的冲突. 重点来了: 所以,如果我们把哪个变量或者函数放在全局作用域中,那 ...

  3. 面向对象day1

    一.什么是面向对象 之前我们学习过面向过程和函数式编程,在讲函数的时候有说过之所以有函数式编程是因为面向过程编程是根据业务逻辑从上到下垒代码,会出现大量代码的重用和臃肿,so,函数式编程将同一功能的代 ...

  4. 在SpringMVC框架下实现文件的 上传和 下载

    在eclipse中的javaEE环境下:导入必要的架包 web.xml的配置文件: <?xml version="1.0" encoding="UTF-8" ...

  5. Big String-POJ2887块状数组

    Time Limit: 1000MS Memory Limit: 131072K Description You are given a string and supposed to do some ...

  6. XMPP环境搭建

    搭建XMPP环境需要几个辅助工具 1.XAMPP     XAMPP(Apache+MySQL+PHP+PERL)是一个功能强大的建 XAMPP 软件站集成软件包.     许多人通过他们自己的经验认 ...

  7. python sys模块

    sy模块主要用于:解析器及环境 命令行参数 python xx.py xx1 xx2注:xx.py: sys.argv[0] 脚本名称 xx1 sys.argv[1] 第1个参数退出程序 sys.ex ...

  8. Node.js开发利器

    开发工具 WebStorm,毫无疑问非他莫属,跨平台,强大的代码提示,支持Nodejs调试,此外还支持vi编辑模式,这点我很喜欢. 做些小型项目用Sublime Text. Browserify:将你 ...

  9. excel单元格内换行

    强制换行:将光标置于拟换行处,按ALT+Enter键,即可强行换行.

  10. 集群节点间网络通信TIPC

    1. TIPC背景介绍 TIPC主要是用于集群网络环境之中,它这个协议有一些前提假设包括: 协议发送的大部分message都是直接到达目的地(无路由): message的传输时间都很短; messag ...