当前位置:网站首页>[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;
}
}
边栏推荐
- Discuss | what preparations should be made before ar application is launched?
- [论文分享] Where’s Crypto?
- zdog. JS rocket turn animation JS special effects
- Chapter 3 business function development (user access project)
- Is it safe to open an online futures account now? How many regular futures companies are there in China?
- Yunjing network technology interview question [Hangzhou multi tester] [Hangzhou multi tester _ Wang Sir]
- “解密”华为机器视觉军团:华为向上,产业向前
- Tips for this week 131: special member functions and ` = Default`
- Chapter 2 building CRM project development environment (building development environment)
- Chapter 3 business function development (safe exit)
猜你喜欢
The highest level of anonymity in C language
Click on the top of today's headline app to navigate in the middle
讨论 | AR 应用落地前,要做好哪些准备?
Download, installation and development environment construction of "harmonyos" deveco
zdog. JS rocket turn animation JS special effects
[principle and technology of network attack and Defense] Chapter 1: Introduction
磁盘存储链式的B树与B+树
线程池和单例模式以及文件操作
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
debian10系统问题总结
随机推荐
Simple configuration of single arm routing and layer 3 switching
将模型的记忆保存下来!Meta&UC Berkeley提出MeMViT,建模时间支持比现有模型长30倍,计算量仅增加4.5%...
通过 Play Integrity API 的 nonce 字段提高应用安全性
小程序中实现付款功能
【demo】循环队列及条件锁实现goroutine间的通信
[network attack and defense principle and technology] Chapter 4: network scanning technology
嵌入式C语言程序调试和宏使用的技巧
Chapter 3 business function development (to remember account and password)
Hash, bitmap and bloom filter for mass data De duplication
清华、剑桥、UIC联合推出首个中文事实核查数据集:基于证据、涵盖医疗社会等多个领域
Introduction of common API for socket programming and code implementation of socket, select, poll, epoll high concurrency server model
Five simple ways to troubleshoot with Stace
Tips of the week 136: unordered containers
手撕Nacos源码(先撕客户端源码)
Learn to make dynamic line chart in 3 minutes!
Usage of PHP interview questions foreach ($arr as $value) and foreach ($arr as $value)
Idea completely uninstalls installation and configuration notes
AntiSamy:防 XSS 攻击的一种解决方案使用教程
Disk storage chain B-tree and b+ tree
能同时做三个分割任务的模型,性能和效率优于MaskFormer!Meta&UIUC提出通用分割模型,性能优于任务特定模型!开源!...