[leetcode] #239 Sliding Window Maximum (Hard)
题意:
给定一个数组数字,有一个大小为k的滑动窗口,它从数组的最左边移动到最右边。你只能在窗口看到k个数字。每次滑动窗口向右移动一个位置。
记录每一次窗口内的最大值,返回记录的值。
思路:
解法一:
一开始用的c++迭代器来模拟窗口,每移动一次就遍历一次找出最大值填入返回数组中。
效率非常低 Runtime: 108 ms, faster than 7.98% of C++
class Solution
{
public:
vector<int> maxSlidingWindow(vector<int> &nums, int k)
{
int len = nums.size();
vector<int> vec; if (len == )
return vec; vector<int>::iterator iter = nums.begin();
for (int i = ; i + k <= len; i++)
{
vec.push_back(*max_element(iter + i, iter + i + k));
}
return vec;
}
};
解法二:
在Discuss中看到大家普遍用的一直优解思路:
用一个双端队列deque保存每一个窗口里的最大值的索引,同时该队列保存的索引为有序的。
Runtime: 60 ms, faster than 45.44% of C++
class Solution
{
public:
vector<int> maxSlidingWindow(vector<int> &nums, int k)
{
deque<int> dq;
vector<int> ans;
for (int i = ; i < nums.size(); i++)
{
if (!dq.empty() && 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;
}
};
[leetcode] #239 Sliding Window Maximum (Hard)的更多相关文章
- [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 ...
- leetcode 239 Sliding Window Maximum
这题是典型的堆排序算法,只是比一般的堆算法多了删除的操作,有两件事需要做: 1 用一个hash表存储从输入数组索引到堆数组(用于实现堆的那个数组)所以的映射,以便在需要删除一个元素的时候能迅速定位到堆 ...
- 【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 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减队列 MultiSet 日期 题目地址:ht ...
- 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题解-----Sliding Window Maximum
题目描述: Given an array nums, there is a sliding window of size k which is moving from the very left of ...
- 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 ...
随机推荐
- 移动Web - 响应式布局开篇
用到的工具: FireFox浏览器 Sublime Text 2 响应式布局定义: 2010年,Ethan Marcotte提出,可查看原文: 通俗地讲就是:百份比布局,根据不同设备显示不同布局: 这 ...
- DelphiRemotePushSender
Sending iOS (and Android) remote push notifications from your Delphi service with the HTTP/2 protoco ...
- Delphi 7学习开发控件(继承TGraphicControl只画一条线)
我们知道使用Delphi快速开发,很大的一方面就是其强大的VCL控件,另外丰富的第三方控件也使得Delphi程序员更加快速的开发出所需要的程序.在此不特别介绍一些概念,只记录自己学习开发控件的步骤.假 ...
- DHTMLEdit控件的安装
xp中自带了DHTMLEdit.ocx, 所以只需安装即可 但是可视化设计面板中没有这个控件,需要我们手动安装一下 具体方式: [import AcitveX control 在控件板上的安装]一. ...
- VS2008下QT整合OGRE
环境配置如下:VS2008 QT版本:4.8.1 OGRE版本: 1.7.4 请先配置好QT for vs2008 : 下载QT VS2008的包,然后下个QT VS的插件 版本必须是VS2008 ...
- Laravel --- Laravel5.3 和 Workerman结合使用(异步)
网上查阅资料有现成和workerman结合的composer组件,但个人感觉不太靠谱,github上star太少,而且怕有问题也不好调,就想自己先试试. 我的办法因为修改要一点Workerman源码, ...
- Storm —— 单机环境搭建
1. 安装环境要求 you need to install Storm's dependencies on Nimbus and the worker machines. These are: Jav ...
- Git 所有常用命令
写的很细致,存: https://blog.csdn.net/Mr_Lewis/article/details/85547057
- tar命令压缩和解压
https://www.cnblogs.com/jyaray/archive/2011/04/30/2033362.html tar命令详解 -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向 ...
- java 字符串内存分配的分析与总结
经常在网上各大版块都能看到对于java字符串运行时内存分配的探讨,形如:String a = "123",String b = new String("123" ...