当前位置:网站首页>[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;
}
}
边栏推荐
- 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的本周学习资源精选【点击标题直接下载】
- 低代码助力企业数字化转型会让程序员失业?
- 通过 Play Integrity API 的 nonce 字段提高应用安全性
- 云景网络科技面试题【杭州多测师】【杭州多测师_王sir】
- 磁盘存储链式的B树与B+树
- 2022年理财有哪些产品?哪些适合新手?
- Using stored procedures, timers, triggers to solve data analysis problems
- 元宇宙带来的创意性改变
- Wireshark分析抓包数据*.cap
猜你喜欢

Discuss | what preparations should be made before ar application is launched?

debian10系统问题总结

Summary of evaluation indicators and important knowledge points of regression problems

The performance and efficiency of the model that can do three segmentation tasks at the same time is better than maskformer! Meta & UIUC proposes a general segmentation model with better performance t

静态路由配置

SD_DATA_RECEIVE_SHIFT_REGISTER
![[paper sharing] where's crypto?](/img/27/9b47bfcdff8307e63f2699d6a4e1b4.png)
[paper sharing] where's crypto?

Industry case | digital operation base helps the transformation of life insurance industry

Hash, bitmap and bloom filter for mass data De duplication

ICer知识点杂烩(后附大量题目,持续更新中)
随机推荐
Nunjuks template engine
线程池和单例模式以及文件操作
[paddleseg source code reading] add boundary IOU calculation in paddleseg validation (1) -- val.py file details tips
科学家首次观察到“电子漩涡” 有助于设计出更高效的电子产品
不能忽略的现货白银短线操作小技巧
回归测试的分类
直播软件搭建,canvas文字加粗
[4500 word summary] a complete set of skills that a software testing engineer needs to master
Classification of regression tests
Chapter 1 Introduction to CRM core business
RIP和OSPF的区别和配置命令
讨论 | AR 应用落地前,要做好哪些准备?
pip相关命令
小试牛刀之NunJucks模板引擎
Learn to make dynamic line chart in 3 minutes!
Save the memory of the model! Meta & UC Berkeley proposed memvit. The modeling time support is 30 times longer than the existing model, and the calculation amount is only increased by 4.5%
[principle and technology of network attack and Defense] Chapter 1: Introduction
嵌入式C语言程序调试和宏使用的技巧
【C语言】字符串函数
[paper sharing] where's crypto?