当前位置:网站首页>[sword finger offer] 59 - I. maximum value of sliding window
[sword finger offer] 59 - I. maximum value of sliding window
2022-07-07 18:37:00 【LuZhouShiLi】
The finger of the sword Offer 59 - I. Maximum sliding window
subject
Given an array nums And the size of the sliding window k, Please find the maximum value in all sliding windows .
Ideas
- Traverse the elements in a given array , If the queue is not empty and the currently inspected element is greater than or equal to the end of the queue element , Then remove the tail element , Until the queue is empty or the current review element is smaller than the new tail element
- When the subscript of the first element is smaller than the left boundary of the sliding window Left Indicates that the team leader element no longer belongs to the sliding window , Remove it from the team head element
- Because array subscript from 0 Start , So when the right edge of the window right + 1>= k, It means that the window has been formed . At this time, the first element of the queue is the maximum value in the window .
Code
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
if(nums.length == 0)
{
return new int[0];
}
// Number of windows
int[] res = new int[nums.length - k + 1];
LinkedList<Integer> queue = new LinkedList<>();
// Traverse the elements in the array , right Represents the right boundary of the sliding window
for(int right = 0; right < nums.length; right++)
{
// If the queue is not empty And the current element is greater than or equal to the tail element Remove the tail element
// until The queue is empty Or the current element is smaller than the new tail element
while(!queue.isEmpty() && nums[right] >= nums[queue.peekLast()]){
queue.removeLast();
}
// Storage element subscript
queue.addLast(right);
// Calculate the left boundary of the window
int left = right - k + 1;
// When the subscript of the first element is less than the left boundary of the sliding window Left
// Indicates that the team leader element is no longer sliding inside the window Remove it from the head of the team
if(queue.peekFirst() < left)
{
queue.removeFirst();
}
// From below 0 Start So when right + 1 >= k It indicates that the sliding window has been formed The first element of the queue is the maximum value of the window
if(right + 1 >= k)
{
res[left] = nums[queue.peekFirst()];// Store the team leader element in res in
}
}
return res;
}
}
边栏推荐
- What skills can you master to be a "master tester" when doing software testing?
- 回归问题的评价指标和重要知识点总结
- [论文分享] Where’s Crypto?
- [C language] string function
- [trusted computing] Lesson 10: TPM password resource management (II)
- 现货白银分析中的一些要点
- Chapter 2 building CRM project development environment (building development environment)
- 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
- Kubernetes DevOps CD工具对比选型
- What are the financial products in 2022? What are suitable for beginners?
猜你喜欢
What skills can you master to be a "master tester" when doing software testing?
Chapter 2 build CRM project development environment (database design)
性能测试过程和计划
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
Kirk Borne的本周学习资源精选【点击标题直接下载】
A few simple steps to teach you how to see the K-line diagram
备份阿里云实例-oss-browser
DataSimba推出微信小程序,DataNuza接受全场景考验? | StartDT Hackathon
上市十天就下线过万台,欧尚Z6产品实力备受点赞
持续测试(CT)实战经验分享
随机推荐
What skills can you master to be a "master tester" when doing software testing?
强化学习-学习笔记8 | Q-learning
gsap动画库
How to clean when win11 C disk is full? Win11 method of cleaning C disk
debian10系统问题总结
[tpm2.0 principle and Application guide] Chapter 5, 7 and 8
简单几步教你如何看k线图图解
Thread pool and singleton mode and file operation
Tsinghua, Cambridge and UIC jointly launched the first Chinese fact verification data set: evidence-based, covering many fields such as medical society
[demo] circular queue and conditional lock realize the communication between goroutines
3分钟学会制作动态折线图!
pip相关命令
Classification of regression tests
Datasimba launched wechat applet, and datanuza accepted the test of the whole scene| StartDT Hackathon
五种网络IO模型
AntiSamy:防 XSS 攻击的一种解决方案使用教程
清华、剑桥、UIC联合推出首个中文事实核查数据集:基于证据、涵盖医疗社会等多个领域
Hutool - 轻量级 DB 操作解决方案
Wireshark analyzes packet capture data * cap
AI 击败了人类,设计了更好的经济机制