当前位置:网站首页>[stack and queue topics] - sliding window
[stack and queue topics] - sliding window
2022-04-23 20:22:00 【[email protected]】
deque
1. The concept of double ended queue :
Ordinary queues can delete elements at the head of the queue , Add elements at the end of the team . For double ended queues , At the head of the team 、 The tail of the team can enter and leave the team .
- Out of line and in line at one end , It's kind of like a stack , After the first out
- There are also restricted double ended queues :
You can join the team at one end 、 Out of line operation , The other end can only join the team ;
You can join the team at one end 、 Out of line operation , The other end can only be out of line .
2. Basic operation of double ended queue :
queue.peek() == queue.peekFirst() Check out the team leader element
queue.peekLast() Check out the end of the team elements
queue.add() == queue.addFirst() Add elements from the head of the team
queue.addLast() Add elements from the end of the team
queue.poll() == queue.pollFirst() Delete team leader element
queue.pollLast() Delete end of team element
LeetCode 239: Maximum sliding window
️ Their thinking :
(1) Create a result set array , The size is nums.length - k + 1
(2) Create a double ended queue , Save the index of the elements put in the queue .
If the queue is empty , Then directly put the element index into the queue ; If the queue is not empty , And the new element is larger than the previous element , Then pop up the last element , Then join the current element into the team
because At present The maximum value in the window must not be smaller than the current element to be entered
(3) Determine whether the current maximum value is still in the sliding window , If it's no longer in the window , To pop the index of this element from the head of the queue
(4) Save the maximum value in the window to the result set array
️ Code section :
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
// Special case handling
if(nums == null || nums.length < 2){
return nums;
}
// Result set array
int [] res = new int [nums.length - k + 1];
// Use a double ended queue to save the array index
LinkedList<Integer> queue = new LinkedList();
// Traversal array
for(int i = 0; i < nums.length; i++){
// If the queue is not empty , The current in-line element is smaller than the next queued element , Then pop up the elements in the team
while(!queue.isEmpty() && nums[queue.peekLast()] <= nums[i]){
queue.pollLast();
}
// Queue the index of the current element
queue.addLast(i);
// Determine whether the value in the queue is valid
if(queue.peek() <= i - k){
queue.poll();
}
// Saves the maximum value in the slider to the result set , Mainly control the first input element
if(i + 1 >= k){
res[i - k + 1] = nums[queue.peek()];
}
}
return res;
}
}
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204232021066473.html
边栏推荐
- NC basic usage 3
- Zdns was invited to attend the annual conference of Tencent cloud basic resources and share the 2020 domain name industry development report
- On BIM data redundancy theory
- Confusion about thread blocking after calling the read () method of wrapper flow
- Commit and ROLLBACK in DCL of 16mysql
- Use the rolling division method to find the maximum common divisor of two numbers
- Investigate why close is required after sqlsession is used in mybatties
- AQS learning
- WordPress plug-in: WP CHINA Yes solution to slow domestic access to the official website
- Mysql database and table building: the difference between utf8 and utf8mb4
猜你喜欢
Mathematical modeling column | Part 5: MATLAB optimization model solving method (Part I): Standard Model
Linux64Bit下安装MySQL5.6-不能修改root密码
[talkative cloud native] load balancing - the passenger flow of small restaurants has increased
On BIM data redundancy theory
Installation and use of NVM
BMP JPEG 图片转换为矢量图像 ContourTrace
Tensorflow 2 basic operation dictionary
Five minutes to show you what JWT is
RT-1052学习笔记 - GPIO架构分析
Some basic configurations in interlij idea
随机推荐
【PTA】整除光棍
SQL Server Connectors By Thread Pool | DTSQLServerTP 插件使用说明
Tencent Qiu Dongyang: techniques and ways of accelerating deep model reasoning
Automatically fill in body temperature and win10 task plan
BMP JPEG 图片转换为矢量图像 ContourTrace
STM32 Basics
Some basic knowledge of devexpress report development
論文寫作 19: 會議論文與期刊論文的區別
【目标跟踪】基于帧差法结合卡尔曼滤波实现行人姿态识别附matlab代码
Building the tide, building the foundation and winning the future -- the successful holding of zdns Partner Conference
Sqoop imports tinyint type fields to boolean type
上海回应“面粉官网是非法网站”:疏于运维被“黑”,警方已立案
DTMF dual tone multi frequency signal simulation demonstration system
Computing the intersection of two planes in PCL point cloud processing (51)
Redis的安装(CentOS7命令行安装)
PCL点云处理之计算两平面交线(五十一)
R语言使用caret包的preProcess函数进行数据预处理:对所有的数据列进行BoxCox变换处理(将非正态分布数据列转换为正态分布数据、不可以处理负数)、设置method参数为BoxCox
R language survival package coxph function to build Cox regression model, ggrisk package ggrisk function and two_ Scatter function visualizes the risk score map of Cox regression, interprets the risk
On BIM data redundancy theory
Monte Carlo py solves the area problem! (save pupils Series)