当前位置:网站首页>【剑指 Offer】59 - I. 滑动窗口的最大值
【剑指 Offer】59 - I. 滑动窗口的最大值
2022-07-07 16:36:00 【LuZhouShiLi】
剑指 Offer 59 - I. 滑动窗口的最大值
题目
给定一个数组 nums 和滑动窗口的大小 k,请找出所有滑动窗口里的最大值。
思路
- 遍历给定数组中的元素,如果队列不为空且当前考察的元素大于等于队尾元素,则将队尾元素移除,直到队列为空或者当前考察元素小于新的队尾元素
- 当队首元素的下标小于滑动窗口的左侧边界Left表示队首元素已经不再属于滑动窗口,将其从队首元素移除
- 由于数组下标从0开始,因此当窗口的右边界right + 1>= k,意味着窗口已经形成。此时队首元素就是该窗口内的最大值。
代码
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
if(nums.length == 0)
{
return new int[0];
}
// 窗口个数
int[] res = new int[nums.length - k + 1];
LinkedList<Integer> queue = new LinkedList<>();
// 遍历数组中元素, right表示滑动窗口右边界
for(int right = 0; right < nums.length; right++)
{
// 如果队列不为空 并且当前的元素大于或者等于队尾元素 将队尾元素移除
// 直到 队列为空 或者当前的元素小于新的队尾元素
while(!queue.isEmpty() && nums[right] >= nums[queue.peekLast()]){
queue.removeLast();
}
// 存储元素下标
queue.addLast(right);
// 计算窗口左侧边界
int left = right - k + 1;
//当队首元素的下标小于滑动窗口左侧边界Left
// 表示队首元素已经不再滑动窗口内部 将其从队首中移除
if(queue.peekFirst() < left)
{
queue.removeFirst();
}
// 下边从0开始 所以当right + 1 >= k 说明滑动窗口已经形成 队首元素就是窗口的最大值
if(right + 1 >= k)
{
res[left] = nums[queue.peekFirst()];// 将队首元素存储到res中
}
}
return res;
}
}
边栏推荐
- CVPR 2022丨学习用于小样本语义分割的非目标知识
- zdog. JS rocket turn animation JS special effects
- [trusted computing] Lesson 10: TPM password resource management (II)
- [principle and technology of network attack and Defense] Chapter 7: password attack technology Chapter 8: network monitoring technology
- Performance test process and plan
- Is it safe to open an online futures account now? How many regular futures companies are there in China?
- 数学分析_笔记_第11章:Fourier级数
- 简单几步教你如何看k线图图解
- The report of the state of world food security and nutrition was released: the number of hungry people in the world increased to 828million in 2021
- 讨论 | AR 应用落地前,要做好哪些准备?
猜你喜欢
Understanding of 12 methods of enterprise management
Thread pool and singleton mode and file operation
AntiSamy:防 XSS 攻击的一种解决方案使用教程
[4500 word summary] a complete set of skills that a software testing engineer needs to master
强化学习-学习笔记8 | Q-learning
清华、剑桥、UIC联合推出首个中文事实核查数据集:基于证据、涵盖医疗社会等多个领域
【C语言】字符串函数
How to clean when win11 C disk is full? Win11 method of cleaning C disk
Click on the top of today's headline app to navigate in the middle
Kubernetes DevOps CD工具对比选型
随机推荐
Do you really understand sticky bag and half bag? 3 minutes to understand it
Interviewer: why is the page too laggy and how to solve it? [test interview question sharing]
[paddleseg source code reading] add boundary IOU calculation in paddleseg validation (1) -- val.py file details tips
How to open an account for wealth securities? Is it safe to open a stock account through the link
Introduction of common API for socket programming and code implementation of socket, select, poll, epoll high concurrency server model
物联网OTA技术介绍
Explain it in simple terms. CNN convolutional neural network
小试牛刀之NunJucks模板引擎
debian10编译安装mysql
Unlike the relatively short-lived industrial chain of consumer Internet, the industrial chain of industrial Internet is quite long
Mui side navigation anchor positioning JS special effect
The highest level of anonymity in C language
Tips of this week 135: test the contract instead of implementation
“解密”华为机器视觉军团:华为向上,产业向前
Some key points in the analysis of spot Silver
List selection JS effect with animation
Tips for this week 131: special member functions and ` = Default`
Five network IO models
回归问题的评价指标和重要知识点总结
行业案例|数字化经营底座助力寿险行业转型