当前位置:网站首页>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;
}
边栏推荐
- PMP工具与技术总结
- Article summary: the basic model of VPN and business types
- MySQL-数据定义语言-DDLdatebase define language
- RSA主要攻击方法
- UE4 制作遇到的问题
- C# | 使用Json序列化对象时忽略只读的属性
- August 22 Promotion Ambassador Extra Reward Rules
- Message Queuing Message Storage Design (Architecture Camp Module 8 Jobs)
- IJCAI2022 | Hybrid Probabilistic Reasoning with Algebraic and Logical Constraints
- 数组问题之《两数之和》以及《三数之和 》
猜你喜欢
(more than 2022 cattle school four) A - Task Computing + dynamic programming (sort)
Interview Blitz 69: Is TCP Reliable?Why?
认真对待每一个时刻
博客系统(完整版)
数组问题之《两数之和》以及《三数之和 》
Mysql基础篇(Mysql数据类型)
What is dynamic programming and what is the knapsack problem
【愚公系列】2022年07月 Go教学课程 023-Go容器之列表
PAT乙级 1002 写出这个数
云服务器下载安装mongo数据库并远程连接详细图文版本(全)
随机推荐
leetcode:126. Word Solitaire II
基于Arduino制作非接触式测温仪
typescript24-类型推论
What is a programming language
怀念故乡的月亮
Advice given by experts with four years of development experience in Flutter tutorial
Pyspark机器学习:向量及其常用操作
【目标检测】YOLOv7理论简介+实践测试
MySQL实践总结-
The method of solving stored procedure table name passing through variable in mysql
【云原生之kubernetes实战】kubernetes集群的检测工具——popeye
时时刻刻保持敬畏之心
UE4 从鼠标位置射出射线检测
程序员代码面试指南 CD15 生成窗口最大值数组
High Numbers | 【Re-integration】Line Area Score 880 Examples
/etc/fstab
lambda
PAT乙级 1002 写出这个数
故乡的素描画
Progressive Reconstruction of Visual Structure for Image Inpainting 论文笔记