[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 ...
随机推荐
- CPU多核控速
初学者很多对自己开发的软件使用硬件资源的时候并不注意,造成写出的东西不是很满意. 一般有两种情况: 1.写的都是同步单线程任务,不管你电脑有多少个核都不关我事 我就用你1个核所以不管怎么样都不会把CP ...
- 专访Rust——由Mozilla开发的系统编程语言(目标人群就是那些纠结的C++程序员,甚至也是他们自己)
Rust是由Mozilla开发的专门用来编写高性能应用程序的系统编程语言.以下是对Rust的创始人——Graydon Hoare的采访. Graydon Hoare,自称为职业编程语言工程师,从200 ...
- CrashRpt_v.1.4.2_vs2008_also_ok
1.windows多线程程序release版崩溃记录工具,便于该如何查找错误. 2.此工具主要用来配置windbug工具,一种排查程序发布版本崩溃这种非常难处理的缺陷的方法,非常棒,amazing! ...
- [每天记录一个Bug]Cell中由于block加载网络请求产生的复用
Bug 出现场景: cell中使用加载图片的网络请求出现复用,截图如下: 复用原因: Cell Model中只有一个用户的uid,所有用户相关信息:例如头像\名称\信息等是通过 ...
- oh my god,写20万数据到Excel只需9秒
还是菜鸟时,在某个.Net项目中,用户需要从业务系统导出Report,而数据量通常都在上万条以上,最初采用的方式就是在服务器端用NPOI生成Excel,把Data一行一行一个Cell一个Cell地写到 ...
- python中的函数名,闭包,迭代器
一.函数名 函数名是一个变量,但它是一个特殊的变量,与括号配合可以执行函数的变量,单纯print()出的是一个内存地址. def func(): print('你说你有点难追') print(func ...
- Docker最全教程之使用Node.js搭建团队技术文档站(二十三)
前言 各种编程语言均有其优势和生态,有兴趣的朋友完全可以涉猎多门语言.在平常的工作之中,也可以尝试选择相对适合的编程语言来完成相关的工作. 在团队技术文档站搭建这块,笔者尝试了许多框架,最终还是选择了 ...
- Redis相关面试题
Reids:单线程+io多路复用机制 Redis与Memcached的区别: 一.memcached值是简单字符串,redis支持hash.set.list等复杂数据类型 二.redis可持久化数据, ...
- Java集合 LinkedList的原理及使用
Java集合 LinkedList的原理及使用 LinkedList和ArrayList一样是集合List的实现类,虽然较之ArrayList,其使用场景并不多,但同样有用到的时候,那么接下来,我们来 ...
- Laravel --- 如何较优雅的使用公用函数
一.创建公用文件 App/Helpers/CommonHelper.php 二.创建Provider php artisan make:provider HelperServiceProvider C ...