当前位置:网站首页>239. 滑动窗口最大值(困难)-单向队列、大顶堆-字节跳动高频题
239. 滑动窗口最大值(困难)-单向队列、大顶堆-字节跳动高频题
2022-04-23 17:32:00 【hequnwang10】
一、题目描述
给你一个整数数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。
返回 滑动窗口中的最大值 。
示例 1:
输入:nums = [1,3,-1,-3,5,3,6,7], k = 3
输出:[3,3,5,5,6,7]
解释:
滑动窗口的位置 最大值
--------------- -----
[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
示例 2:
输入:nums = [1], k = 1
输出:[1]
二、解题
单向队列
首先这题需要注意两个点
- 维护滑动窗口长度值,滑动窗口值为k;
- 维护单向队列保存下标值,左右两个下标,[左大,右下],左边的下标值代表的数值最大,右边最小,使队列中的元素从大到小。
当当前值大于单向队列中的队尾值时,删除队尾值,使单向队列的元素值从大到小顺序保存,如果左右下标的区间值超过了滑动窗口值,则更新左边界。
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
int length = nums.length;
//队列保存的是下标值,下标值的区间来维护滑动窗口的大小长度
Deque<Integer> queue = new LinkedList<>();
//初始化滑动窗口
for(int i = 0;i<k;i++){
//保证滑动窗口的下标[左大,右小]。当前值大于队尾元素 则更新最大值
while(!queue.isEmpty() && nums[i] >= nums[queue.peekLast()]){
queue.pollLast();
}
//向队尾添加元素
queue.addLast(i);
}
int[] res = new int[length - k + 1];
res[0] = nums[queue.peekFirst()];
for(int i = k;i<length;i++){
//如果当前值大于队尾元素,则直接出队,
while(!queue.isEmpty() && nums[i] >= nums[queue.peekLast()]){
queue.pollLast();
}
//将当前下标存入队尾
queue.addLast(i);
//维护滑动窗口的大小
while(queue.peekFirst() <= i - k){
queue.pollFirst();
}
res[i-k+1] = nums[queue.peekFirst()];
}
return res;
}
}
大顶堆
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
//大顶堆
int n = nums.length;
PriorityQueue<int[]> pq = new PriorityQueue<>(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;
}
}
版权声明
本文为[hequnwang10]所创,转载请带上原文链接,感谢
https://blog.csdn.net/hequnwang10/article/details/124341517
边栏推荐
- JS, entries(), keys(), values(), some(), object Assign() traversal array usage
- 给 el-dialog 增加拖拽功能
- Shell - introduction, variables, and basic syntax
- Self use learning notes - connected and non connected access to database
- Freecodecamp ---- budget & category exercise
- flink 学习(十二)Allowed Lateness和 Side Output
- Generation of barcode and QR code
- C# Task. Delay and thread The difference between sleep
- Shell-cut命令的使用
- Oninput one function to control multiple oninputs (take the contents of this input box as parameters) [very practical, very practical]
猜你喜欢

ASP. NET CORE3. 1. Solution to login failure after identity registers users

Scope and scope chain in JS

.Net Core3. 1 use razorengine NETCORE production entity generator (MVC web version)

1-4 configuration executable script of nodejs installation

1217_使用SCons生成目标文件

为什么有些人说单片机简单,我学起来这么吃力?

2. Electron's HelloWorld
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]

XTask与Kotlin Coroutine的使用对比

线性代数感悟之2
随机推荐
PC电脑使用无线网卡连接上手机热点,为什么不能上网
Shell-awk命令的使用
【WPF绑定3】 ListView基础绑定和数据模板绑定
1-4 configuration executable script of nodejs installation
Signalr can actively send data from the server to the client
How to manually implement the mechanism of triggering garbage collection in node
ASP. Net core JWT certification
Using quartz under. Net core -- general properties and priority of triggers for [5] jobs and triggers
QT modification UI does not take effect
Generating access keys using JSON webtoken
Use of shell awk command
C语言程序设计之函数的构造
Understanding of RPC core concepts
Promise (I)
Clickhouse - data type
2. Electron's HelloWorld
Come out after a thousand calls
Bottom processing of stack memory in browser
快时钟同步慢时钟域下的异步控制信号slow clk to fast clk
C语言函数详解