当前位置:网站首页>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;
}
边栏推荐
- 7 行代码搞崩溃 B 站,原因令人唏嘘!
- How to promote new products online?
- mysql中解决存储过程表名通过变量传递的方法
- Flutter Tutorial 02 Introduction to Flutter Desktop Program Development Tutorial Run hello world (tutorial includes source code)
- Risk strategy important steps of tuning method
- typescript24-类型推论
- In the shake database, I want to synchronize the data of the source db0 to the destination db5, how to set the parameters?
- 数据比对功能调研总结
- 【目标检测】YOLOv7理论简介+实践测试
- typescript25 - type assertion
猜你喜欢

Logitech Mouse Experience Record

Pyspark机器学习:向量及其常用操作

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

(Codeforce 757)E. Bash Plays with Functions(积性函数)

(2022牛客多校四)K-NIO‘s Sword(思维)

Risk strategy important steps of tuning method

律师解读 | 枪炮还是玫瑰?从大厂之争谈元宇宙互操作性

button remove black frame

typescript19-对象可选参数

(2022牛客多校四)H-Wall Builder II(思维)
随机推荐
lambda
【堆】小红的数组
Flutter Tutorial 01 Configure the environment and run the demo program (tutorial includes source code)
25. Have you been asked these three common interview questions?
Write a method to flatten an array and deduplicate and sort it incrementally
李迟2022年7月工作生活总结
typescript21 - Comparison of Interfaces and Type Aliases
(2022牛客多校四)D-Jobs (Easy Version)(三维前缀或)
【愚公系列】2022年07月 Go教学课程 025-递归函数
动态规划 01背包
状态压缩dp
PMP 项目质量管理
Game Theory (Depu) and Sun Tzu's Art of War (42/100)
Dry goods!How to Construct SRv6-TE Performance Test Environment Using Instrumentation
typescript24-类型推论
高数 | 【重积分】线面积分880例题
typescript25-类型断言
Lawyer Interpretation | Guns or Roses?Talking about Metaverse Interoperability from the Battle of Big Manufacturers
【目标检测】YOLOv7理论简介+实践测试
PMP 项目资源管理