作者: 负雪明烛
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. Vue3 中有哪些值得深究的知识点?

    众所周知,前端技术一直更新很快,这不 vue3 也问世这么久了,今天就来给大家分享下vue3中值得注意的知识点.喜欢的话建议收藏,点个关注! 1.createApp vue2 和 vue3 在创建实例 ...

  2. 日常Java 2021/10/1

    正则表达式 \cx匹配由x指明的控制字符.例如,lcM匹配一个Control-M或回车符.x的值必须为A-Z或a-z之一.否则,将c视为一个原义的'℃'字符.\f匹配--个换页符.等价于\xOc和\c ...

  3. day09 文件属性

    day09 文件属性 昨日回顾 yum底层原理: 第一步:执行yum install nginx安装命令 第二步:yum去/etc/yum.repos.d这个目录中 第三步:根据/etc/yum/re ...

  4. MediaPlayer详解

    [1]MediaPlayer 详细使用细则 [2]MediaPlayer使用详解_为新手准备 [3]MediaPlayer 概览

  5. 高效读取大文件,再也不用担心 OOM 了!

    内存读取 第一个版本,采用内存读取的方式,所有的数据首先读读取到内存中,程序代码如下: Stopwatch stopwatch = Stopwatch.createStarted(); // 将全部行 ...

  6. 利用unordered_map维护关联数据

    在leetcode上刷339题Evaluate Division(https://leetcode.com/problems/evaluate-division/#/description)时在脑中过 ...

  7. 【编程思想】【设计模式】【测量模式Testability】Setter_injection

    Python版 https://github.com/faif/python-patterns/blob/master/dft/setter_injection.py #!/usr/bin/pytho ...

  8. MySQL安装详细教程(数据库免安装版)

    MySQL安装详细教程(数据库免安装版)mysql-5.7.33-winx64.zip 一.软件下载 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,属于 Oracle 旗下产 ...

  9. 使用RabbitMQ搭建MQTT服务

    由于近期公司需要搭建一套物联网采集环境,底层设备采用MQTT协议传输数据.服务器环境为linux,考虑到现有环境已经有RabbitMQ环境,Rabbit是基于AMQP协议开发的一套高效的消息传输队列. ...

  10. win10更新后任务栏卡死 的原因和解决办法

    @ 目录 现象: 原因: 第一步:断网并关闭资讯和兴趣 第二步:卸载更新 第三步:关闭win10自动更新 第四步:永久关闭资讯和兴趣 现象: win10 更新后,开机任务栏卡死,点开始反应,设置页面无 ...