当前位置:网站首页>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;
}
边栏推荐
- Progressive Reconstruction of Visual Structure for Image Inpainting 论文笔记
- 高数 | 【重积分】线面积分880例题
- ModuleNotFoundError: No module named ‘tensorflow.keras‘报错信息的解决方法
- Excuse me, only primary key columns can be queried using sql in table storage. Does ots sql not support non-primary keys?
- MySQL-DML语言-数据库操作语言-insert-update-delete-truncate
- button remove black frame
- MySQL-数据定义语言-DDLdatebase define language
- 7月编程排行榜来啦!这次有何新变化?
- PMP 项目质量管理
- 【无标题】
猜你喜欢
基于ProXmoX VE的虚拟化家庭服务器(篇一)—ProXmoX VE 安装及基础配置
typescript26 - literal types
Typescript22 - interface inheritance
MySQL-数据定义语言-DDLdatebase define language
(2022牛客多校四)N-Particle Arts(思维)
What is dynamic programming and what is the knapsack problem
API设计笔记:pimpl技巧
Step by step hand tearing carousel Figure 3 (nanny level tutorial)
干货!如何使用仪表构造SRv6-TE性能测试环境
typescript20-接口
随机推荐
MySQL-数据操作-分组查询-连接查询-子查询-分页查询-联合查询
PMP 相关方管理必背总结
万字逐行解析与实现Transformer,并进行德译英实战(三)
MySQL实践总结-
故乡的素描画
Visual Studio提供的 Command Prompt 到底有啥用
typescript26 - literal types
button remove black frame
出现Command ‘vim‘ is available in the following places,vim: command not found等解决方法
云服务器下载安装mongo数据库并远程连接详细图文版本(全)
typescript21-接口和类型别名的对比
Li Chi's work and life summary in July 2022
FFmpeg 搭建本地屏幕录制环境
UE4 制作遇到的问题
MLP neural network, GRNN neural network, SVM neural network and deep learning neural network compare and identify human health and non-health data
Mysql基础篇(约束)
力扣(LeetCode)212. 单词搜索 II(2022.07.31)
Invalid classes inferred from unique values of `y`. Expected: [0 1 2], got [1 2 3]
TIM登陆时提示00001(TIM00001)
leetcode:126. Word Solitaire II