当前位置:网站首页>[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
边栏推荐
- SQL Server connectors by thread pool 𞓜 instructions for dtsqlservertp plug-in
- Development of Matlab GUI bridge auxiliary Designer (functional introduction)
- 【PTA】L1-002 打印沙漏
- Paper writing 19: the difference between conference papers and journal papers
- PCL点云处理之基于PCA的几何形状特征计算(五十二)
- Monte Carlo py solves the area problem! (save pupils Series)
- R语言ggplot2可视化:ggplot2可视化散点图并使用geom_mark_ellipse函数在数据簇或数据分组的数据点周围添加椭圆进行注释
- Customize timeline component styles
- JDBC tool class jdbcfiledateutil uploads files and date format conversion, including the latest, simplest and easiest way to upload single files and multiple files
- R language ggplot2 visualization: ggplot2 visualizes the scatter diagram and uses geom_ mark_ The ellipse function adds ellipses around data points of data clusters or data groups for annotation
猜你喜欢
Actual measurement of automatic ticket grabbing script of barley network based on selenium (the first part of the new year)
selenium. common. exceptions. WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT
SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
考研英语唐叔的语法课笔记
【PTA】L1-002 打印沙漏
网络通信基础(局域网、广域网、IP地址、端口号、协议、封装、分用)
. Ren -- the intimate artifact in the field of vertical Recruitment!
Es error: request contains unrecognized parameter [ignore_throttled]
Monte Carlo py solves the area problem! (save pupils Series)
上海回应“面粉官网是非法网站”:疏于运维被“黑”,警方已立案
随机推荐
[graph theory brush question-4] force deduction 778 Swimming in a rising pool
微信中金财富高端专区安全吗,证券如何开户呢
JDBC tool class jdbcconutil gets the connection to the database
PIP installation package reports an error. Could not find a version that satisfies the requirement pymysql (from versions: none)
SIGIR'22 "Microsoft" CTR estimation: using context information to promote feature representation learning
NC basic usage 2
SQL Server connectors by thread pool 𞓜 instructions for dtsqlservertp plug-in
Click an EL checkbox to select all questions
Sqoop imports data from Mysql to HDFS using lzop compression format and reports NullPointerException
The flinkcdc reports an error: but this is no longer available on the server
SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
DNS cloud school rising posture! Three advanced uses of authoritative DNS
Change the material of unity model as a whole
R语言survival包coxph函数构建cox回归模型、ggrisk包ggrisk函数和two_scatter函数可视化Cox回归的风险评分图、解读风险评分图、基于LIRI数据集(基因数据集)
Record: call mapper to report null pointer Foreach > the usage of not removing repetition;
Building the tide, building the foundation and winning the future -- the successful holding of zdns Partner Conference
Mysql database and table building: the difference between utf8 and utf8mb4
Some basic configurations in interlij idea
Computing the intersection of two planes in PCL point cloud processing (51)
ABAQUS script email auto notification