当前位置:网站首页>剑指 Offer 59 - II. 队列的最大值
剑指 Offer 59 - II. 队列的最大值
2022-07-17 02:27:00 【BugMaker-shen】

使用一个queue和一个deque,queue进行正常的push和pop操作,deque辅助找到当前队列中最大值
入队时,如果当前元素value小于deque队尾元素,直接把value入队deque;如果当前元素value大于deque队尾元素,从deque队尾开始,把小于value的元素全部从队尾出队。(deque内元素是降序的)
出队时,只要queue队首元素小于deque队首元素,直接出队queue队首元素即可;若queue队首元素和deque队首元素相等了,则两者一起出队。deque队首元素表示的是,queue不把该值出队,deque队首元素都是最大的
class MaxQueue {
public:
queue<int> q;
deque<int> deq;
MaxQueue() {
}
int max_value() {
if(q.empty()){
return -1;
}
return deq.front();
}
void push_back(int value) {
while(!deq.empty() && value > deq.back()){
deq.pop_back();
}
q.push(value);
deq.push_back(value);
}
int pop_front() {
if(q.empty()){
return -1;
}
if(deq.front() == q.front()){
deq.pop_front();
}
int ans = q.front();
q.pop();
return ans;
}
};
边栏推荐
- 【C语言】0基础教程——文件操作(未完待续)
- KubeCon + CloudNativeCon Europe 2022
- options has an unknown property ‘before‘
- Boston house price analysis assignment summary
- 数字孪生-第二章、数字孪生技术
- Matlab绘制激活函数sigmoid,Relu
- Chapter 4 用户数据分析
- Neural network learning notes 2.2 -- write a simple convolution neural network image classifier with MATLAB
- VGG (Visual Geometry Group)
- 让企业数字化砸锅和IT主管背锅的软件供应链安全风险指北
猜你喜欢

options has an unknown property ‘before‘

Chapter 4 用户数据分析

Detailed explanation of arrow function and this direction

Neural network learning notes 2.2 -- write a simple convolution neural network image classifier with MATLAB

Reptile learning (5): teach you reptile requests practice hand in hand

Properties of Gaussian distribution (including code)

MySQL addition, deletion, query and modification (basic)

如何在自动化测试中使用MitmProxy获取数据返回?

Swagger

一种鲁棒变形卷积神经网络图像去噪
随机推荐
windows10:vscode下go语言的适配
神器网站目录,全都是刚需好用的网站
渗透测试-01信息收集
MySQL addition, deletion, query and modification (basic)
Chapter I Introduction
Through openharmony compatibility evaluation, the big brother development board and rich teaching and training resources have been ready
Receiver operating curve
缩短饿了么tabs 组件线条宽度
Edge detection method -- first order edge detection
机器学习库Scikit-Learn(线性模型、岭回归、插入一列数据(insert)、提取所需列、向量机(SVM)、聚类)
线程的私有存储空间
Method of realizing horizontal and vertical centering of unknown width and height elements
Machine learning library scikit learn (linear model, ridge regression, insert a column of data, extract the required column, vector machine (SVM), clustering)
automake中文手册_incomplete
KlakNdI 同步画面简单使用
运算符、赋值语句、结构说明语句
Ext JS的数字类型处理及便捷方法
【LeetCode】735. 行星碰撞
Deep learning experiment notes
web语义化(强调标签-em-斜体)(重点强调标签-strong-粗体)(自定义列表:dl、dt、dd)