原题链接

题意:

给定一个数组数字,有一个大小为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. Qt信号量QSemaphore(在线程里使用,结合生产者消费者的问题)

    Qt中的信号量是由QSemaphore类提供的,信号量可以理解为对互斥量功能的扩展,互斥量只能锁定一次而信号量可以获取多次,它可以用来保护一定数量的同种资源.acquire()函数用于获取n个资源,当 ...

  2. jquery中的DOM操作集锦

    1,查找节点: 1 2 var $li = $("ul li:eq(1)");//查找元素 $li.attr("title"); //查找元素的属性值   2, ...

  3. asp.net mvc中使用jquery H5省市县三级地区选择控件

    地区选择是项目开发中常用的操作,本文讲的控件是在手机端使用的选择控件,不仅可以用于实现地区选择,只要是3个级别的选择都可以实现,比如专业选择.行业选择.职位选择等.效果如下图所示: 附:本实例asp. ...

  4. Spring AOP APIS

    1:Pointcut API in Spring (1):切点接口定义 org.springframework.aop.Pointcut接口是中心接口.用来将Advice(通知)定位到特定的类和方法. ...

  5. Hadoop 学习之路(五)—— Hadoop集群环境搭建

    一.集群规划 这里搭建一个3节点的Hadoop集群,其中三台主机均部署DataNode和NodeManager服务,但只有hadoop001上部署NameNode和ResourceManager服务. ...

  6. a元素变成块状元素点击之后删除出现背景

    a { text-decoration: none; background: none; -webkit-tap-highlight-color: transparent; } a:hover { - ...

  7. JS 数据类型分析及字符串的方法

    1.js数据类型分析 (1)基础类型:string.number.boolean.null.undefined (2)引用类型:object-->json.array... 2.点运算  xxx ...

  8. Fish and Oh My Fish in Ubuntu

    After install Fish shell, then install Oh My Fish . Oh My Fish(shortly OMF) can make our Fish shell ...

  9. 使用docker运行GitLab

    从docker镜像拉取代码,docker pull gitlab/gitlab-ce:latest. 创建/srv/gitlab目录sudo mkdir /srv/gitlab 启动GitLab CE ...

  10. Programming In Lua 第七章

    1, 2, 3, 第三点需要讲解下:for循环中,allwords函数是工厂函数,只调用一次.for循环的每次遍历,都会调用工厂函数返回的闭包函数.这样就能遍历一个文件的每一行的每一个单词. 4, 我 ...