当前位置:网站首页>[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;
}
}
边栏推荐
- 保证接口数据安全的10种方案
- sqlite sql 异常 near “with“: syntax error
- Five network IO models
- Usage of PHP interview questions foreach ($arr as $value) and foreach ($arr as $value)
- Tips for this week 131: special member functions and ` = Default`
- Datasimba launched wechat applet, and datanuza accepted the test of the whole scene| StartDT Hackathon
- [network attack and defense principle and technology] Chapter 4: network scanning technology
- Sports Federation: resume offline sports events in a safe and orderly manner, and strive to do everything possible for domestic events
- Will domestic software testing be biased
- 国内的软件测试会受到偏见吗
猜你喜欢

上市十天就下线过万台,欧尚Z6产品实力备受点赞

Chapter 3 business function development (safe exit)

强化学习-学习笔记8 | Q-learning

简单几步教你如何看k线图图解

Chapter 2 building CRM project development environment (building development environment)

gsap动画库
![[PaddleSeg源码阅读] PaddleSeg Validation 中添加 Boundary IoU的计算(1)——val.py文件细节提示](/img/f2/b6a0e5512b35cf1b695a8feecd0895.png)
[PaddleSeg源码阅读] PaddleSeg Validation 中添加 Boundary IoU的计算(1)——val.py文件细节提示

回归问题的评价指标和重要知识点总结

Kubernetes DevOps CD工具对比选型
![[4500 word summary] a complete set of skills that a software testing engineer needs to master](/img/82/acae52928b3ab48e9ecbf4ec436e5e.jpg)
[4500 word summary] a complete set of skills that a software testing engineer needs to master
随机推荐
SQLite SQL exception near "with": syntax error
Taffydb open source JS database
[demo] circular queue and conditional lock realize the communication between goroutines
Five network IO models
CVPR 2022丨学习用于小样本语义分割的非目标知识
不能忽略的现货白银短线操作小技巧
Kirk borne's selection of learning resources this week [click the title to download directly]
PIP related commands
体总:安全有序恢复线下体育赛事,力争做到国内赛事应办尽办
Chapter 1 Introduction to CRM core business
The highest level of anonymity in C language
nest.js入门之 database
[principle and technology of network attack and Defense] Chapter 7: password attack technology Chapter 8: network monitoring technology
zdog. JS rocket turn animation JS special effects
Tips for short-term operation of spot silver that cannot be ignored
讨论| 坦白局,工业 AR 应用为什么难落地?
What skills can you master to be a "master tester" when doing software testing?
[trusted computing] Lesson 13: TPM extended authorization and key management
Five simple ways to troubleshoot with Stace
保证接口数据安全的10种方案