【LeetCode】239. Sliding Window Maximum 解题报告(Python&C++)
作者: 负雪明烛
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
kis always valid,1 ≤ k ≤ inputarray’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++)的更多相关文章
- 【原创】leetCodeOj --- Sliding Window Maximum 解题报告
天,这题我已经没有底气高呼“水”了... 题目的地址: https://leetcode.com/problems/sliding-window-maximum/ 题目内容: Given an arr ...
- [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 ...
- [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 ...
- leetcode 239 Sliding Window Maximum
这题是典型的堆排序算法,只是比一般的堆算法多了删除的操作,有两件事需要做: 1 用一个hash表存储从输入数组索引到堆数组(用于实现堆的那个数组)所以的映射,以便在需要删除一个元素的时候能迅速定位到堆 ...
- [leetcode] #239 Sliding Window Maximum (Hard)
原题链接 题意: 给定一个数组数字,有一个大小为k的滑动窗口,它从数组的最左边移动到最右边.你只能在窗口看到k个数字.每次滑动窗口向右移动一个位置. 记录每一次窗口内的最大值,返回记录的值. 思路: ...
- 【LeetCode】239. Sliding Window Maximum
Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving fr ...
- 【刷题-LeetCode】239. Sliding Window Maximum
Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving from ...
- 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 ...
- (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 ...
随机推荐
- dart系列之:还在为编码解码而烦恼吗?用dart试试
目录 简介 为JSON编码和解码 UTF-8编码和解码 总结 简介 在我们日常使用的数据格式中json应该是最为通用的一个.很多时候,我们需要把一个对象转换成为JSON的格式,也可以说需要把对象编码为 ...
- Shell $()、${}、$[]、$(())
目录 Shell中的 $().${}.$[].$(()) $().${} 替换 ${} 变量内容的替换.删除.取代 数组 $[].$(()) 运算符 Shell中的 $().${}.$[].$(()) ...
- 日常Java(测试 (二柱)修改版)2021/9/22
题目: 一家软件公司程序员二柱的小孩上了小学二年级,老师让家长每天出30道四则运算题目给小学生做. 二柱一下打印出好多份不同的题目,让孩子做了.老师看了作业之后,对二柱赞许有加.别的老师闻讯, 问二柱 ...
- 【风控算法】一、变量分箱、WOE和IV值计算
一.变量分箱 变量分箱常见于逻辑回归评分卡的制作中,在入模前,需要对原始变量值通过分箱映射成woe值.举例来说,如"年龄"这一变量,我们需要找到合适的切分点,将连续的年龄打散到不同 ...
- absurd, abundant
absurd How: absolutely, completely, clearly, faintly, manifestly, obviously, patently, quite, rather ...
- 02-爬取http://www.allitebooks.org/网站,获取图片url,书名,简介,作者
import requests from lxml import etree from bs4 import BeautifulSoup import json class BookSpider(ob ...
- 30个类手写Spring核心原理之环境准备(1)
本文节选自<Spring 5核心原理> 1 IDEA集成Lombok插件 1.1 安装插件 IntelliJ IDEA是一款非常优秀的集成开发工具,功能强大,而且插件众多.Lombok是开 ...
- python下载openpyxl
直接下载openpyxl报错 ERROR: Command errored out with exit status 1: python setup.py egg_info Check the log ...
- 利用extern共享全局变量
方法: 在xxx.h中利用extern关键字声明全局变量 extern int a; 在xxx.cpp中#include<xxx.h> 再定义 int a; 赋不赋初值无所谓,之后该全局变 ...
- mysql 索引 零记
索引算法 二分查找法/折半查找法 伪算法 : 1. 前提,数据需要有序 2. 确定数据中间元素 K 3. 比如目标元素 A与K的大小 3.1 相等则找到 3.2 小于时在左区间 3.3 大于时在右 ...