当前位置:网站首页>2022/07/21 --- maximum value of sliding window;
2022/07/21 --- maximum value of sliding window;
2022-07-22 13:23:00 【City Pig】
List of articles
Title Description
remaining problems :2022/07/20— Convert a string to an integer ; Maximum sliding window
The maximum value of the queue
Their thinking
For yesterday's window maximum : A monotonous queue , My understanding is that ------ Maintain a queue , Consider from two aspects , Take company recruitment as an example , One side , Come in, a new man , Then fire those who are weaker than the newcomers ; Another aspect , The number of the team is stable , When more than a certain number of people , Will consider firing the older . That is to say , New people have strong ability , Cut the weak , There are too many people in the team , Cut the old . According to this idea . The maximum version of the sliding window 3 The code for is as follows .
For the maximum value of the queue : I didn't understand the topic at first . I read the comments later to understand what the topic is talking about . in general , Is to build two queues , A queue stores all the values , A queue is used to slide windows , Traverse and save the maximum value of this queue . The code will not let go .
Code implementation
The maximum version of the sliding window 3 Code for
package cz;
import java.util.ArrayDeque;
import java.util.Deque;
public class maxSlidingWindow_0721 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int []nums = {
1,3,1,2,0,5};
int k = 3;
int [] res=maxSlidingWindow(nums,k);
System.out.print(res[0]);
}
public static int[] maxSlidingWindow(int[] nums, int k) {
// Determine whether it is an empty array
if(nums==null || nums.length==0) {
return new int [0];
}
// Create a new array to save the results
int [] res=new int [nums.length-k+1];
// Create a new double ended queue to store the array
Deque<Integer> queue=new ArrayDeque<>();
for(int i=0,j=0;i<nums.length;i++) {
// Ensure that the maximum position difference in the queue is k-1
if(!queue.isEmpty()&& i-queue.peek()>=k) {
// Remove the first element of the double ended queue
queue.poll();
}
// When the current element is larger than the end of the queue , Remove the element at the end of the queue
while(!queue.isEmpty()&& nums[i]>nums[queue.peekLast()]) {
queue.pollLast();
}
// Insert the position of the current element at the end of the queue
queue.offer(i);
if(i>=k-1) {
// Ensure that the team leader is the maximum
res[j++]=nums[queue.peek()];
}
}
return res;
}
}
Experience : Whether learning or writing , It is precisely because of the impulse of righteousness at the bottom of my heart , Will continue to achieve better themselves .
边栏推荐
猜你喜欢
[MySQL]数据库基础操作
(Applied Intelligence-2022)TransGait: 基于多模态的步态识别与集合Transformer
How to select the type of ankerui intelligent miniature circuit breaker?
“新能源+储能“从数字孪生开始,图扑将智慧电力做到极致
PMP每日一练 | 考试不迷路-7.21
ERP系统在元器件贸易企业中的应用
ACL - ijcai - Sigir top Conference Paper presentation Conference (ais 2022) Note 2: Analysis and interpretability
Simple crud of SSM
"Xiaodeng in operation and maintenance" ensures gdpr compliance by auditing user activities and internal changes in the ad domain
老薛主机磁盘空间满了怎么办
随机推荐
QScriptEngine官方说明文档
The point rotates clockwise around the center of the grid by 90 ° 180 ° 270 ° coordinate change
Félicitations pour la tenue réussie du Forum sur la gestion de l'innovation de produits « innovation et autonomisation » le 16 juillet
Applet project summary
elemen点击时,修改走马灯的播放索引
Clickhouse engine -materializemysql
C. Doremy's IQ (greedy)
v7底部栏fragment
数商云:供应商多场景趋势下,服装企业如何打造灵活应用的SRM管理体系?
Daily question-leetcode814-binary tree pruning recursion
: use of class in the project
Elephant Swap的LaaS方案迅速崛起,构建全新DeFi2.0协议
Chart drawing summary
[FAQ] common reasons and solutions for the failure of in app payment services to pull up the payment page
SeekTiger的Okaleido有大动作,生态通证STI会借此爆发?
同一个浏览器不同窗口登录不同账号,窗口切换时,页面刷新账号变更为最后一次登录的账号
(ICLR-2021)一幅图像相当于16X16个words:大规模图像识别的Transformer
机器学习-集成学习
mySQL基本函数
Procurement control: shorten the procurement cycle, reduce costs and increase efficiency from the source