当前位置:网站首页>栈与队列——239. 滑动窗口最大值
栈与队列——239. 滑动窗口最大值
2022-07-25 23:55:00 【向着百万年薪努力的小赵】
1 题目描述
给你一个整数数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。
返回 滑动窗口中的最大值 。
2 题目示例

示例 2:
输入:nums = [1], k = 1
输出:[1]
3 题目提示
1 <= nums.length <= 105
-104 <= nums[i] <= 104
1 <= k <= nums.length
4 思路
队列:
对于「最大值」,我们可以想到—种非常合适的数据结构,那就是优先队列(堆),其中的大根堆可以帮助我们实时维护—系列元素中的最大值。
对于本题而言,初始时,我们将数组nums的前k个元素放入优先队列中。每当我们向右移动窗口时,我们就可以把一个新的元素放入优先队列中,此时堆顶的元素就是堆中所有元素的最大值。然而这个最大值可能并不在滑动窗口中,在这种情况下,这个值在数组nums 中的位置出现在滑动窗口左边界的左侧。因此,当我们后续继续向右移动窗口时,这个值就永远不可能出现在滑动窗口中了,我们可以将其永久地从优先队列中移除。我们不断地移除堆顶的元素,直到其确实出现在滑动窗口中。此时,堆顶元素就是滑动窗口中的最大值。为了方便判断堆顶元素与滑动窗口的位置关系,我们可以在优先队列中存储二元组(num, indez),表示元素num在数组中的下标为indez。
复杂度分析
时间复杂度:O(n log n),其中n是数组nums的长度。在最坏情况下,数组nums中的元素单调递增,那么最终优先队列中包含了所有元素,没有元素被移除。由于将一个元素放入优先队列的时间复杂度为O(log n),因此总时间复杂度为O(n logn)。
空间复杂度:O(n),即为优先队列需要使用的空间。这里所有的空间复杂度分析都不考虑返回的答案需要的O(n)空间,只计算额外的空间使用。
5 我的答案
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
int n = nums.length;
PriorityQueue<int[]> pq = new PriorityQueue<int[]>(new Comparator<int[]>() {
public int compare(int[] pair1, int[] pair2) {
return pair1[0] != pair2[0] ? pair2[0] - pair1[0] : pair2[1] - pair1[1];
}
});
for (int i = 0; i < k; ++i) {
pq.offer(new int[]{
nums[i], i});
}
int[] ans = new int[n - k + 1];
ans[0] = pq.peek()[0];
for (int i = k; i < n; ++i) {
pq.offer(new int[]{
nums[i], i});
while (pq.peek()[1] <= i - k) {
pq.poll();
}
ans[i - k + 1] = pq.peek()[0];
}
return ans;
}
}
边栏推荐
- 【MUDUO】EventLoopThreadPool
- The process of finding free screen recording software - I didn't expect win10 to come with this function
- Programmer interview Golden Classic 4.12 summation path
- Exercise (2) create a set to store the elements "1", "$", "2", "$", "3", "$", "4"“
- Good news under the epidemic
- Unexpected dubug tricks
- ShardingSphere数据分片
- Article 75: writing skills of academic papers
- After entering www.baidu.com in the address bar
- Promise resolve callback hell, async await modifier
猜你喜欢

redis-基本数据类型(String/list/Set/Hash/Zset)

Storage of data in memory

VSCode格式化Json文件
![[Muduo] EventLoop event cycle](/img/80/824c7061d58796d454be0c438e257c.png)
[Muduo] EventLoop event cycle

Payment terms in SAP message No. vg202 IDoc e1edk18 have been transferred: check data

Shardingsphere data slicing
![[learning notes] solid works operation record](/img/f7/0535b473755643ce32996f00fa5554.png)
[learning notes] solid works operation record

面试重点——传输层的TCP协议

程序环境和预处理

C language (high level) program environment and preprocessing
随机推荐
程序环境和预处理
意向不到的Dubug妙招
The expression of flag=false if (flag) {return} timer=null if (timer) {return} in the throttle valve has been unclear
R语言安装教程 | 图文介绍超详细
Lua脚本编写Wireshark插件解析第三方私有协议
十大排序之快速排序
SIGIR '22 recommendation system paper graph network
Find the cause of program dead cycle through debugging
Good news under the epidemic
Promise resolve callback hell, async await modifier
[learning notes] unreal 4 engine introduction (III)
What is parity? How to use C language?
Zhiniu stock -- 09
静态代理+动态代理
Scroll series
【英雄哥七月集训】第 24天: 线性树
行为型模式之责任链模式
Exercise (3) create a list set (both ArrayList and LinkedList)
Static agent + dynamic agent
抽丝剥茧C语言(高阶)程序环境和预处理