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 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:
- How about using a data structure such as deque (double-ended queue)?
- The queue size need not be the same as the window’s size.
- 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* -- 滑动窗口的最大值的更多相关文章
- 【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]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 滑动窗口最大值
Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...
- 239 Sliding Window Maximum 滑动窗口最大值
给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口 k 内的数字.滑动窗口每次只向右移动一位.例如,给定 nums = [1,3,-1,-3, ...
- POJ 2823 Sliding Window & Luogu P1886 滑动窗口
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 66613 Accepted: 18914 ...
- 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 ...
- 【LeetCode】239. Sliding Window Maximum 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减队列 MultiSet 日期 题目地址:ht ...
- (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 ...
随机推荐
- 为何iPhone6 Plus的逻辑分辨率是2208×1242,屏幕实际分辨率却是1920×1080
因为除了iPhone 6+以外,其他所有iPhone的DPI是一致的,都是326,用@2x的素材.但是6+的实际DPI是401,理论上苹果应该用401/326 * @2x=@2.46x的素材,但是这个 ...
- Hibernate 基础配置及常用功能(一)
本来是想等全部框架测试完以后再统一发布的,但是随着测试的一点点增加感觉把需要叙述的东西放在一起终将会是一场灾难.所以还是打算分成几章来描述,其中还包括一些有待解决的问题.短期很难腾出时间来仔细阅读Hi ...
- json,pickle
json 将python基本数据类型转换成字符串形式 import json dict = {'k1':'v1'} result = json.dumps(dict) print(result,ty ...
- git管理测试生产环境代码
利用post-update实现简单钩子 #!/bin/bash cd /www/test || exit #进入指定的目录 unset GIT_DIR #清楚环境变量 git checkout mas ...
- Masonry的使用
1.//添加了这个宏,就不用带mas_前缀了 #define MAS_SHORTHAND //添加了这个宏,equalTo就等于mas_equalTo #define MAS_SHORYHAND_G ...
- 移动APP的开发需求分析
一.项目概况 项目名称为上海地铁游.本项目是以上海地铁为线索,开发的一个移动APP.主要目的是帮助用户实现根据当前位置选择最方便的地铁旅游点和旅游推荐,方便出行,让更多人可以借助地铁的便利去认识和体验 ...
- ASP.NET Core 使用 AutoFac 注入 DbContext
DI 1.0 -- 通过 RegisterInstance 注入 一开始,并不是很懂 AutoFac 的用法,又因为要使用特定的构造器和参数来初始化 DbContext,所以我想到的办法就是使用 Re ...
- Bar codes in NetSuite Saved Searches(transport/reprint)
THIS IS A COPY FROM BLOG Ways of incorporating Bar Codes into your Netsuite Saved Searches. Code ...
- 【前端】CommonJS的模块加载机制
CommonJS的模块加载机制 CommonJS模块的加载机制是,输入的是被输出的值的拷贝.也就是说,一旦输出一个值,模块内部的变化就影响不到这个值. 例如: // lib.js var counte ...
- 回归分析法&一元线性回归操作和解释
用Excel做回归分析的详细步骤 一.什么是回归分析法 "回归分析"是解析"注目变量"和"因于变量"并明确两者关系的统计方法.此时,我们把因 ...