当前位置:网站首页>[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;
}
}
边栏推荐
- 4种常见的缓存模式,你都知道吗?
- Discuss | frankly, why is it difficult to implement industrial AR applications?
- [论文分享] Where’s Crypto?
- 五种网络IO模型
- Automated testing: a practical skill that everyone wants to know about robot framework
- pip相关命令
- Classification of regression tests
- 低代码助力企业数字化转型会让程序员失业?
- Chapter 1 Introduction to CRM core business
- Yearning-SQL审核平台
猜你喜欢
持续测试(CT)实战经验分享
回归测试的分类
Skills of embedded C language program debugging and macro use
Nunjuks template engine
Download, installation and development environment construction of "harmonyos" deveco
通过 Play Integrity API 的 nonce 字段提高应用安全性
低代码助力企业数字化转型会让程序员失业?
Idea completely uninstalls installation and configuration notes
[trusted computing] Lesson 13: TPM extended authorization and key management
Kubernetes DevOps CD工具对比选型
随机推荐
Yunjing network technology interview question [Hangzhou multi tester] [Hangzhou multi tester _ Wang Sir]
性能测试过程和计划
云景网络科技面试题【杭州多测师】【杭州多测师_王sir】
强化学习-学习笔记8 | Q-learning
Kirk Borne的本周学习资源精选【点击标题直接下载】
低代码助力企业数字化转型会让程序员失业?
标准ACL与扩展ACL
Afghan interim government security forces launched military operations against a hideout of the extremist organization "Islamic state"
sqlite sql 异常 near “with“: syntax error
Download, installation and development environment construction of "harmonyos" deveco
PHP面试题 foreach($arr as &$value)与foreach($arr as $value)的用法
SD_DATA_RECEIVE_SHIFT_REGISTER
磁盘存储链式的B树与B+树
Ten thousand words nanny level long article -- offline installation guide for datahub of LinkedIn metadata management platform
Rules for filling in volunteers for college entrance examination
CVPR 2022丨学习用于小样本语义分割的非目标知识
Tsinghua, Cambridge and UIC jointly launched the first Chinese fact verification data set: evidence-based, covering many fields such as medical society
Will domestic software testing be biased
String type, constant type and container type of go language
Chapter 2 build CRM project development environment (database design)