原题链接

题意:

给定一个数组数字,有一个大小为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 设置背景图片3种方法(三种方法:QPalette调色板,paintEvent,QSS)

    方法1. setStylSheet{"QDialog{background-image:url()"}}  //使用styleSheet 这种方法的好处是继承它的dialog都会自 ...

  2. Spring之bean生命始末

    可以为Bean定制初始化后的生命行为,也可以为Bean定制销毁前的生命行为.举例:ba06包.首先,这些方法需要在Bean类中事先定义好:是方法名随意的public void方法. 其次,在配置文件的 ...

  3. python实现常用查找算法

    http://www.cnblogs.com/feixuelove1009/p/6148357.html

  4. Mybatis_One

    Mabatis的概述 JavaEE开发是分层的:表现层 业务层 持久层 框架(Framework)是整个或部分系统的可重用设计,表现为一组抽象构件及构件实例间交互的方法;另一种定义认为,框架是可被应用 ...

  5. Docker安装MySql-挂载外部数据和配置

    环境 CentOS:7 Docker:1.31.1 MySql:5.7   拷贝mysql配置文件 1.首先创建mysql容器 -p : -e MYSQL\_ROOT\_PASSWORD= -d my ...

  6. JS工具整理

    1.获取今日日期:摘抄地址:https://www.cnblogs.com/carekee/articles/1678041.html getTodayFmt('yyyy-MM-dd') getTod ...

  7. 一道关于String的面试题,新鲜出炉,刚被坑过,趁热!!

    很多人都会答错的一道关于String的题目,究竟有什么难度? 我们一起来看一道关于String的面试题,准确说是改编的面试题! 准备好啦?在放大招之前先来一个小招式 String s1 = new S ...

  8. Nginx查看并发连接数

    Nginx查看并发连接 通过界面查看 通过界面查看通过web界面查看时Nginx需要开启status模块,也就是安装Nginx时加上 --with-http_stub_status_module 然后 ...

  9. 学习 GitHub 有什么好处?

    layout: post title: "学习 GitHub 有什么好处?" date: 2018-04-15 19:20:20 +0800 --- 鸣谢:王顶 老师(河北经贸大学 ...

  10. TypeScript算法与数据结构-数组篇

    数组是数据结构中最简单,也是使用最广泛的一种.在原生的js中,数组给我们提供了很多方便的操作方法,比如push(), pop(), shift(), unshift().但是出于对数据结构的学习,我们 ...