当前位置:网站首页>Sliding window maximum value problem
Sliding window maximum value problem
2022-06-10 19:16:00 【Starlight technician】
Sliding window optimization problem
Basic ideas
When calculating the maximum value of the array window , You need to maintain a two-way queue ; For the maximum value problem, it is necessary to ensure the absolute decrease of the queue in the maintenance process ;
- step : Two The pointer L and R;
1) The pointer R When you move right , Add the passed index to the queue , And determine the operation according to the existing index in the queue , When adding elements to the end of the queue , Make sure that the element corresponding to the index value to be added is absolutely smaller than the element corresponding to the index value at the end of the queue ; If you don't do that , So take the end of the queue Index value popup for , Add... Until the conditions are met ;
2) The pointer L When you move right ,L The index value corresponding to the element passed by the pointer has expired in the queue , Judge whether the invalid element is the first element of the current queue , If so , Just pop up from the head of the team ;
Call : Maintain the index corresponding to the largest element of the current window ;
Example

#include<iostream>
#include<deque>
#include<vector>
using namespace std;
int main()
{
vector<int> res;
deque<int> que_idx;
int S = 3;// Window size
vector<int> in_vec = {
4,3,5,4,3,3,6,7 };
for (int i = 0; i < S; i++)
{
while(!que_idx.empty() && in_vec[que_idx.back()] <= in_vec[i])
que_idx.pop_back();
que_idx.push_back(i);
}
res.push_back(in_vec[que_idx.front()]);
for (int j = 1, k = S; j < in_vec.size() && k < in_vec.size(); j++, k++)
{
if (j-1 == que_idx.front())
{
que_idx.pop_front();
}
while (!que_idx.empty() && in_vec[que_idx.back()] <= in_vec[k])
que_idx.pop_back();
que_idx.push_back(k);
res.push_back(in_vec[que_idx.front()]);
}
for (int m = 0; m < res.size(); m++)
{
cout << res[m] << " ";
}
return 0;
}
边栏推荐
- RK1126 新添加一个模块
- WordPress 6.0 “Arturo阿图罗” 发布
- Super simple course design SSM student management system (including simple addition, deletion, modification and query of source code)
- MySQL (17 after class exercises)
- How to correctly understand the real-time nature of Bi?
- 2022.05.27(LC_647_回文子串)
- Leecode27977 double finger needling
- Rk1126 adds a new module
- Adobe Premiere基礎-工具使用(選擇工具,剃刀工具,等常用工具)(三)
- c(指针02)
猜你喜欢
随机推荐
C (pointer-02)
Dynamic SQL of DB2 SQL pl
Detailed explanation of Lora module wireless transceiver communication technology
最长上升子序列(LIS)洛谷
Google Earth engine (GEE) -- Copernicus atmosphere monitoring (CAMs) global aerosol AOI near real-time observation data set
Adobe Premiere基礎-工具使用(選擇工具,剃刀工具,等常用工具)(三)
Introduction to ad18 device library import
2022.05.29(LC_6078_重排字符形成目标字符串)
【数据库语言SPL】写着简单跑得又快的数据库语言 SPL
2022.05.29(LC_6079_价格减免)
Adobe Premiere Foundation (animation) (VII)
2022.05.28(LC_516_最长回文子序列)
数据库防火墙的性能和高可用性分析
第6章 关系数据理论练习
Cross domain error: when allowcredentials is true, allowedorigins cannot contain the special value "*“
端午“沉浸式云旅游”怎么玩?即构助力“直播+”新场景落地
Design and implementation of online ordering system based on SSM Rar (project source code)
Pits encountered during the use of ETL (ETL Chinese garbled)
Adobe Premiere基础特效(卡点和转场)(四)
基于JSP的医院预约挂号平台设计与开发.zip(论文+项目源码)









