当前位置:网站首页>滑动窗口最值问题
滑动窗口最值问题
2022-06-10 17:44:00 【星光技术人】
基础思想
在求数组窗口最值的时候,需要维护一个双向的队列;对于最大值问题需要保证维护过程中队列绝对递减;
- 步骤:两个 指针L和R;
1) 指针R右移的时候,将经过的索引加入队列,并根据队列里边已经存在的索引来决定操作,在往队列尾部加入元素的时候,一定要保证待加入的索引值对应的元素绝对小于队列尾部索引值对应的元素;如果不满足条件,那么就把队列尾部 的索引值弹出,直到满足条件的时候加入;
2) 指针L右移的时候,L指针经过的元素对应的索引值在队列中已经失效,判断该失效元素是否为目前队列的队首元素,如果是的话,就从队首弹出;
点拨:维护队首元素为目前窗口的最大元素对应的索引;
例题

#include<iostream>
#include<deque>
#include<vector>
using namespace std;
int main()
{
vector<int> res;
deque<int> que_idx;
int S = 3;//窗口大小
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;
}
边栏推荐
- Noise line h5js effect realized by canvas
- Abbexa 无细胞 DNA 试剂盒说明书
- 阅读micropyton源码-添加C扩展类模块(4)
- 搭建在线帮助中心,轻松帮助客户解决问题
- Detailed explanation of MySQL windowing function
- 堆利用之chunk extend: HITCON tranining lab13
- XML & XPath parsing
- Talk about those things about telecommuting, participate in the essay solicitation, receive the contribution fee and win the grand prize!
- 系统需要把所有文件扫描一遍,并尝试识别视频的封面
- 小程序积分商城如何实现营销目的
猜你喜欢

Library for adding progress bar during training --tqdm

yml文件配置参数定义字典和列表

c语言---8 初识常见关键字

Leetcode 875. Coco, who likes bananas

Abbexa 8-OHdG CLIA 试剂盒解决方案

Wireshark learning notes (I) common function cases and skills

ACL2022 | bert2BERT:参数复用的高效预训练方法,显著降低超大模型的训练成本

安装Linux系统的MySQL,在xshell中遇见的问题

AOE network critical path

PCA principal component analysis tutorial (origin analysis & drawing, without R language)
随机推荐
苹果期待的「无密码时代」,真能实现吗?
Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE tha
c语言---13 循环语句while
关于cmake和gcc的安装的记录
mysql备份和shell脚本手动执行没问题,crontab定时执行失败
Abbexa低样本量鸡溶菌酶 C (LYZ) ELISA 试剂盒
Memory pool principle I (based on the whole block)
canvas发散的粒子h5动画js特效
小程序积分商城如何实现营销目的
换根呀呀啊呀
领导提拔你的原因,只有这点最真实,其他都是瞎扯!
Generate XML based on annotations and reflection
【技术分析】探讨大世界游戏的制作流程及技术——前期流程篇
第七部分:第二课 顾问通用技能-如何安装和卸载SAP ERP系统客户端
记录一个超级乌龙的智障bug,也许能帮助类似我的白吃
PCA principal component analysis tutorial (origin analysis & drawing, without R language)
Analysis of transfer Refund Scheme in e-commerce industry
Leetcode 875. Coco, who likes bananas
Domestic cosmetics, lost 618
【ceph】ceph配置源码分析|common/config.*