作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/sliding-window-maximum/

题目描述

Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Return the max sliding window.

Example:

Input: nums = [1,3,-1,-3,5,3,6,7], and k = 3
Output: [3,3,5,5,6,7] Explanation: Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 3
1 [3 -1 -3] 5 3 6 7 3
1 3 [-1 -3 5] 3 6 7 5
1 3 -1 [-3 5 3] 6 7 5
1 3 -1 -3 [5 3 6] 7 6
1 3 -1 -3 5 [3 6 7] 7

Note:

  • You may assume k is always valid, 1 ≤ k ≤ input array’s size for non-empty array.

Follow up:

  • Could you solve it in linear time?

题目大意

求一个滑动窗口中的最大值。

解题方法

单调递减队列

这个题是剑指offer的题目,做法挺多,我使用的是单调递减双向队列解决。

设定一个大小为k的单调递减双向队列,时刻保持队列是单调递减的,即如果从最右边加入了一个较大的数字,需要从右开始退队列,退到队列中剩余的数字都比该数字大位置,此时队列是单调递减的。如果队列的大小达到了k,则应该把队列最前面的数字(其实是之前区间的最大值)删除掉。

时间复杂度是O(N).

python代码如下:

class Solution(object):
def maxSlidingWindow(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""
que = collections.deque() # [[i, num]]
res = []
for i, num in enumerate(nums):
if que and i - que[0][0] >= k:
que.popleft()
while que and que[-1][1] <= num:
que.pop()
que.append([i, num])
if i >= k - 1:
res.append(que[0][1])
return res

MultiSet

在使用这个方法前,我们从这个题目入手。这个题目想让我们得到一个区间里面的最大值,每次这个区间在一次操作中增加一个值、(可能)去掉一个值。那么我们想到如何求一个区间的最大值?简单的方法是使用遍历区间的方式,时间复杂度是O(k),但是既然每次最多只会更改两个数字,没必要遍历整个区间求最大值,于是会想到set/multiset这种结构,C++中的set/multi是使用红黑树实现的,会对内部的元素排序。set会进行去重,而multiset不去重。因此,我们可以使用multiset这个结构,每次新加入一个元素,则会自动排序,最大值的位置是rbegin();如果元素个数达到了k,则把该区间最左边的元素去除,使用st.find(nums[i - k])找到最左边元素的位置,并删除即可。

时间复杂度是O(N*log(k)),每次插入和删除操作是log(k)时间复杂度。

class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
vector<int> res;
multiset<int> st;
for (int i = 0; i < nums.size(); ++i) {
if (st.size() >= k) st.erase(st.find(nums[i - k]));
st.insert(nums[i]);
if (i >= k - 1)
res.push_back(*st.rbegin());
}
return res;
}
};

参考资料:https://www.cnblogs.com/grandyang/p/4656517.html

日期

2019 年 9 月 14 日 —— 假期的生活就是不规律

【LeetCode】239. Sliding Window Maximum 解题报告(Python&C++)的更多相关文章

  1. 【原创】leetCodeOj --- Sliding Window Maximum 解题报告

    天,这题我已经没有底气高呼“水”了... 题目的地址: https://leetcode.com/problems/sliding-window-maximum/ 题目内容: Given an arr ...

  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滑动窗口最大值

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

  4. leetcode 239 Sliding Window Maximum

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

  5. [leetcode] #239 Sliding Window Maximum (Hard)

    原题链接 题意: 给定一个数组数字,有一个大小为k的滑动窗口,它从数组的最左边移动到最右边.你只能在窗口看到k个数字.每次滑动窗口向右移动一个位置. 记录每一次窗口内的最大值,返回记录的值. 思路: ...

  6. 【LeetCode】239. Sliding Window Maximum

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

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

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

  8. 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 ...

  9. (heap)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 ...

随机推荐

  1. Bedtools如何比较两个参考基因组注释版本的基因?

    目录 问题 思路 问题 原问题来自:How to calculate overlapping genes between two genome annotation versions? 其实可分为两个 ...

  2. 关于单倍型和Phasing

    单倍型,即单倍体基因型,概念很好理解. 单倍型分型的过程就称之Phasing,定相或基因分型. Phasing的意义,在人类疾病遗传和动植物群体遗传中非常重要.也是imputation的必经过程. v ...

  3. python 封装、绑定

    目录 python 封装.绑定 1.数据.方法的封装 2.隐藏属性 3.开放接口 4.绑定方法 1.对象的绑定 2.类的绑定(classmethod) 3.非绑定方法(staticmethod) 4. ...

  4. UE4之Slate: App启动与最外层Runtime结构

    UE4版本:4.24.3源码编译: Windows10 + VS开发环境 Slate为一套自定义UI框架,其绘制直接依赖的是OpenGL.DirectX这样的硬件加速AIP;可以理解为一个单独的2D图 ...

  5. A Child's History of England.9

    But, first, as it was important to know how numerous those pestilent Danes were, and how they were f ...

  6. 100个Shell脚本——【脚本5】数字求和

    [脚本5]数字求和 编写shell脚本,要求输入一个数字,然后计算出从1到输入数字的和,要求,如果输入的数字小于1,则重新输入,直到输入正确的数字为止,示例: 一.脚本 #!/bin/bash whi ...

  7. c学习 - 第七章:数组

    7.3.6 字符串处理函数 (1).puts(字符数组) 字符串输出到终端 (2).gets(字符数组) 从标准输入获取字符串(包括空格) (3).strcat(字符数组1,字符数组2) 连接两个字符 ...

  8. ORACLE 加大日志文件

    --新建临时日志文件alter database add logfile group 4 ('/u01/app/oracle/oradata/orcl/redo04.log') size 10m;al ...

  9. When does compiler create default and copy constructors in C++?

    In C++, compiler creates a default constructor if we don't define our own constructor (See this). Co ...

  10. 【Linux】【Services】【SaaS】Docker+kubernetes(2. 配置NTP服务chrony)

    1. 简介 1.1. 这次使用另外一个轻量级的NTP服务,chrony.这是openstack推荐使用的ntp服务. 1.2. 官方网站:https://chrony.tuxfamily.org/ 2 ...