当前位置:网站首页>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;
}
边栏推荐
- 【kali-信息收集】枚举——DNS枚举:DNSenum、fierce
- Excel record of integer programming optimization model to solve the problem
- 风险策略调优中重要的三步分析法
- TIM登陆时提示00001(TIM00001)
- MySQL-数据操作-分组查询-连接查询-子查询-分页查询-联合查询
- mysql中解决存储过程表名通过变量传递的方法
- In the shake database, I want to synchronize the data of the source db0 to the destination db5, how to set the parameters?
- What is a programming language
- 基于STM32设计的UNO卡牌游戏(双人、多人对战)
- Dry goods!How to Construct SRv6-TE Performance Test Environment Using Instrumentation
猜你喜欢

2022-07-31: Given a graph with n points and m directed edges, you can use magic to turn directed edges into undirected edges, such as directed edges from A to B, with a weight of 7.After casting the m

typescript22-接口继承

MySQL-数据定义语言-DDLdatebase define language

微软 Win10 照片磁贴后的又一“刺客”,谷歌 Chrome 浏览器将在新标签页展示用户照片

怀念故乡的面条

IJCAI2022 | Hybrid Probabilistic Reasoning with Algebraic and Logical Constraints

【愚公系列】2022年07月 Go教学课程 024-函数

This article takes you to understand the past and present of Mimir, Grafana's latest open source project

故乡的素描画

剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
随机推荐
PMP工具与技术总结
律师解读 | 枪炮还是玫瑰?从大厂之争谈元宇宙互操作性
Advice given by experts with four years of development experience in Flutter tutorial
PMP 项目质量管理
万字逐行解析与实现Transformer,并进行德译英实战(二)
typescript23-元组
Optional parameters typescript19 - object
Game Theory (Depu) and Sun Tzu's Art of War (42/100)
动态规划 01背包
UE4 模型OnClick事件不生效的两种原因
项目风险管理必备内容总结
Progressive Reconstruction of Visual Structure for Image Inpainting 论文笔记
scheduleWithFixedDelay和scheduleAtFixedRate的区别
25. 这三道常见的面试题,你有被问过吗?
MySQL实践总结-
EntityFramework saves to SQLServer decimal precision is lost
[target detection] YOLOv7 theoretical introduction + practical test
怀念故乡的月亮
mysql中解决存储过程表名通过变量传递的方法
y83.第四章 Prometheus大厂监控体系及实战 -- prometheus告警机制进阶(十四)