当前位置:网站首页>[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;
}
}
边栏推荐
- socket編程之常用api介紹與socket、select、poll、epoll高並發服務器模型代碼實現
- Mobile app takeout ordering personal center page
- Five network IO models
- Wireshark分析抓包数据*.cap
- 通过 Play Integrity API 的 nonce 字段提高应用安全性
- Chapter 3 business function development (safe exit)
- Using stored procedures, timers, triggers to solve data analysis problems
- Classification of regression tests
- 标准ACL与扩展ACL
- The highest level of anonymity in C language
猜你喜欢

Improve application security through nonce field of play integrity API

idea彻底卸载安装及配置笔记

Yearning-SQL审核平台

现货白银分析中的一些要点

CVPR 2022丨学习用于小样本语义分割的非目标知识

将模型的记忆保存下来!Meta&UC Berkeley提出MeMViT,建模时间支持比现有模型长30倍,计算量仅增加4.5%...

线程池和单例模式以及文件操作
![[principle and technology of network attack and Defense] Chapter 6: Trojan horse](/img/2f/bd35ca141fad5c85f78fd6340ada1d.png)
[principle and technology of network attack and Defense] Chapter 6: Trojan horse

讨论| 坦白局,工业 AR 应用为什么难落地?

Automated testing: a practical skill that everyone wants to know about robot framework
随机推荐
云安全日报220707:思科Expressway系列和网真视频通信服务器发现远程攻击漏洞,需要尽快升级
小程序中实现付款功能
Chapter 2 build CRM project development environment (database design)
Tips for this week 140: constants: safety idioms
Introduction de l'API commune de programmation de socket et mise en œuvre de socket, select, Poll et epoll
[network attack and defense principle and technology] Chapter 4: network scanning technology
体总:安全有序恢复线下体育赛事,力争做到国内赛事应办尽办
AI defeated mankind and designed a better economic mechanism
Tips for this week 134: make_ Unique and private constructors
通过 Play Integrity API 的 nonce 字段提高应用安全性
How to open an account for wealth securities? Is it safe to open a stock account through the link
磁盘存储链式的B树与B+树
zdog. JS rocket turn animation JS special effects
Industry case | digital operation base helps the transformation of life insurance industry
Static routing configuration
备份阿里云实例-oss-browser
Personal best practice demo sharing of enum + validation
行业案例|数字化经营底座助力寿险行业转型
NAT地址转换
[tpm2.0 principle and Application guide] Chapter 5, 7 and 8