原题链接

题意:

给定一个数组数字,有一个大小为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)的更多相关文章

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

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

  3. leetcode 239 Sliding Window Maximum

    这题是典型的堆排序算法,只是比一般的堆算法多了删除的操作,有两件事需要做: 1 用一个hash表存储从输入数组索引到堆数组(用于实现堆的那个数组)所以的映射,以便在需要删除一个元素的时候能迅速定位到堆 ...

  4. 【LeetCode】239. Sliding Window Maximum

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

  5. 【刷题-LeetCode】239. Sliding Window Maximum

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

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

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

  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题解-----Sliding Window Maximum

    题目描述: Given an array nums, there is a sliding window of size k which is moving from the very left of ...

  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. Azure 云助手主要功能

    实时查询: 实时查询Azure云服务的用量和账单,还可以将重点服务固定在首页“我的监控”页面,方便快速查看. 随时管控: 随时随地对云服务管理和配置(例如,重启.更改虚拟机). 多个账户: 随心切换多 ...

  2. python正则表达式模块

    正则表达式是对字符串的最简约的规则的表述.python也有专门的正则表达式模块re. 正则表达式函数 释义 re.match() 从头开始匹配,匹配失败返回None,匹配成功可通过group(0)返回 ...

  3. abp(net core)+easyui+efcore仓储系统——定义仓储并实现 (四)

    abp(net core)+easyui+efcore仓储系统目录 abp(net core)+easyui+efcore仓储系统——ABP总体介绍(一) abp(net core)+easyui+e ...

  4. Django 强大的ORM之增删改查

    Django orm         Django——orm进阶 测试Django中的orm 表结构: models.py class User(models.Model): name = model ...

  5. c/c++基础内容回顾

    1.1基本数据类型 1.1.1 变量的定义 变量类型 变量名 变量类型 变量名=初值: 注意:不能以C语言标识符作为变量名,变量名的第一个字符必须是字母或者下划线,其它位置字符必须是字母.数字.下划线 ...

  6. php抽奖概率程序

    抽奖概率思想: 1.给每一个奖项设置要给概率数,如下面所有奖品综合设置为100,iphone5s是5,也就是5% 2.然后通过php生成随机数函数生成一个在总概率之间的随机数 如:抽第一个奖品5s的时 ...

  7. 记录微信浏览器里word链接点击没反应的bug

    有用户反应点击下载附件时没有反应,让用户把该下载链接复制到微信对话框中,发现点击该链接仍然无反应,但是在内置的手机浏览器中打开是正常的而且可以下载. 链接地址,有需要的可以拿去进行测试: http:/ ...

  8. asp.net core 2.2 生产环境直接更新View页面并立即生效

    有的时候我们会直接在生产环境上更新页面文件,比如更改了JS代码,CSS代码或页面的文案,布局等.这种没有改到后台代码的情况一般就是直接发布页面文件了,在asp.net core 2.2以前的版本(没有 ...

  9. SpringMVC_Two

    SpringMVC_Two 响应数据和结果视图 创建工厂 导坐标: </load-on-startup> </servlet> <servlet-mapping> ...

  10. SQL Server温故系列(4):SQL 查询之集合运算 & 聚合函数

    1.集合运算 1.1.并集运算 UNION 1.2.差集运算 EXCEPT 1.3.交集运算 INTERSECT 1.4.集合运算小结 2.聚合函数 2.1.求行数函数 COUNT 2.2.求和函数 ...