当前位置:网站首页>Code Interview Guide for Programmers CD15 Generating an Array of Windowed Maximums
Code Interview Guide for Programmers CD15 Generating an Array of Windowed Maximums
2022-08-01 04:49:00 【Go to bed early and feel good hh】
题目链接:点击这里
首先要明确的是,The index is stored in the double-ended queue.同时,It is necessary to always ensure that the value of the array element corresponding to the index in the queue decreases gradually from the beginning to the end.

ri Used to traverse the array sequentially from left to right a[],同时 ri 也是滑动窗口的最右索引.
li 是滑动窗口的最左索引,即 li = ri - w + 1.

The steps to find the maximum value of the sliding window are as follows:
如果
a[ri] >= a[队尾],则不断删除队尾,直到a[队尾] > a[ri]为止.将
ri加入队尾.当
li < 0的时候,意味着此时还没有出现一个完整的滑动窗口,直接跳过即可.当
li >= 0的时候,如果队头过期,就移除队头,然后记录此时滑动窗口的最大值为a[队头].
#include <iostream>
#include <cstdio>
#include <deque>
using namespace std;
const int N = 1000010;
int a[N];
int main()
{
int n, w;
scanf("%d%d", &n, &w);
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
deque<int> q;
for (int ri = 0; ri < n; ri++)
{
// 如果a[ri] >= a[队尾],则不断删除队尾
while (!q.empty() && a[ri] >= a[q.back()])
{
q.pop_back();
}
// 将ri加入队尾
q.push_back(ri);
int li = ri - w + 1;
// 当li < 0的时候,意味着此时还没有出现一个完整的滑动窗口,直接跳过即可
if (li < 0)
{
continue;
}
// 如果队头过期,就移除队头
if (q.front() < li)
{
q.pop_front();
}
// 记录此时滑动窗口的最大值为a[队头]
printf("%d ", a[q.front()]);
}
return 0;
}
边栏推荐
- Message queue design based on mysql
- IJCAI2022 | Hybrid Probabilistic Reasoning with Algebraic and Logical Constraints
- Visual Studio提供的 Command Prompt 到底有啥用
- PMP工具与技术总结
- 基于ProXmoX VE的虚拟化家庭服务器(篇一)—ProXmoX VE 安装及基础配置
- typescript28 - value of enumeration type and data enumeration
- 怀念故乡的面条
- Excel做题记录——整数规划优化模型
- 阿叶的目标
- 【云原生之kubernetes实战】kubernetes集群的检测工具——popeye
猜你喜欢

typescript24-类型推论

typescript28 - value of enumeration type and data enumeration

(2022牛客多校四)N-Particle Arts(思维)

Message Queuing Message Storage Design (Architecture Camp Module 8 Jobs)

(more than 2022 cattle school four) A - Task Computing + dynamic programming (sort)

风险策略调优中重要的三步分析法

Mysql基础篇(约束)

剑指 Offer 68 - I. 二叉搜索树的最近公共祖先

C# | 使用Json序列化对象时忽略只读的属性

The difference between scheduleWithFixedDelay and scheduleAtFixedRate
随机推荐
PMP 项目资源管理
Invalid classes inferred from unique values of `y`. Expected: [0 1 2], got [1 2 3]
Game Theory (Depu) and Sun Tzu's Art of War (42/100)
(2022牛客多校四)K-NIO‘s Sword(思维)
Article summary: the basic model of VPN and business types
How to promote new products online?
UE4 从鼠标位置射出射线检测
Logitech Mouse Experience Record
lambda
怀念故乡的面条
阿叶的目标
数组问题之《两数之和》以及《三数之和 》
请问表格储存中用sql只能查询到主键列,ots sql非主键不支持吗?
初识shell脚本
typescript24-类型推论
产品经理访谈 | 第五代验证码的创新与背景
ICML2022 | Deep Dive into Permutation-Sensitive Graph Neural Networks
高数 | 【重积分】线面积分880例题
UE4 制作遇到的问题
Dynamic Programming 01 Backpack