当前位置:网站首页>剑指 Offer 59 - II. 队列的最大值
剑指 Offer 59 - II. 队列的最大值
2022-06-29 19:37:00 【Yake1965】
剑指 Offer 59 - II. 队列的最大值
class MaxQueue {
Queue<Integer> q;
Deque<Integer> dq;
public MaxQueue() {
this.q = new LinkedList<>();
this.dq = new LinkedList<>();
}
public int max_value() {
return this.dq.isEmpty() ? -1 : this.dq.peek();
}
public void push_back(int value) {
this.q.add(value);
// 单调队列 非严格递减,保留相等的副本。
while(!this.dq.isEmpty() && value > this.dq.peekLast()){
dq.pollLast();
}
this.dq.add(value); // 添加元素
}
public int pop_front() {
if(this.q.isEmpty()) return -1;
int x = this.q.poll();
if(x == this.dq.peek()) this.dq.pop(); // 通过值的判断
return x;
}
}
// 数组模拟
class MaxQueue {
int[] q = new int[10000];
int[] dq = new int[10000];
int i = 0, j = 0, left = 0, top = 0;
public MaxQueue() {
}
public int max_value() {
return i == j ? -1 : this.dq[left];
}
public void push_back(int value) {
q[j++] = value;
// 单调队列 非严格递减,保留相等的副本。
while(left <= top && value > dq[top]){
top --;
}
this.dq[++top] = value;
}
public int pop_front() {
if(i == j) return -1;
int x = q[i++];
if(x == dq[left]) left++; // 通过值的判断
return x;
}
}
边栏推荐
- CAD Assistant - 3D模型格式转换利器
- Common knowledge of ECS security settings
- The developer task center is online! Thousands of yuan of gifts!
- C语言数组专题训练
- 14.04 million! Sichuan provincial human resources and social security department relational database and middleware software system upgrade procurement bidding!
- @Sneakythlows annotation
- nacos 问题
- Win11系统小组件打不开?Win11系统小组件无法打开解决方法
- STM32CubeMX 学习(6)外部中断实验
- Have you mastered all the testing methods of technology to ensure quality and software testing?
猜你喜欢
MSYQL, redis, mongodb visual monitoring tool grafana

KDD 2022 | 協同過濾中考慮錶征對齊和均勻性

Kdd 2022 | prise en compte de l'alignement et de l'uniformité des représentations dans le Filtrage collaboratif

How is the combination of convolution and transformer optimal?

AI场景存储优化:云知声超算平台基于 JuiceFS 的存储实践

创作者基金会 6 月份亮点

软件测试逻辑覆盖相关理解

C语言数组专题训练

Physical verification LVS process and Technology (Part I)

Game Maker 基金会呈献:归属之谷
随机推荐
Where is the win11 installation permission set? Win11 installation permission setting method
NLP 类问题建模方案探索实践
2022年理财利率都降了,那该如何选择理财产品?
高能直播,大咖云集!邀你共启BizDevOps探索之路。
自动获取本地连接及网络地址修改
Connaissance générale des paramètres de sécurité du serveur Cloud
【️爬虫必备->Scrapy框架从黑铁到王者️】初篇——万字博文详解(建议收藏)
powershell命令仅输出目录列表
IP error problem of PHP laravel using AWS load balancer
Creators foundation highlights in June
Sophomore majoring in software engineering, the previous learning situation is not very good. How to plan the follow-up development route
电脑ssd硬盘怎么安装使用
3-2主机发现-三层发现
【网络方向实训】-企业园区网络设计-【Had Done】
4-2 port banner information acquisition
有了这4个安全测试工具,对软件安全测试say so easy!
Lingyun going to sea | Wenhua online &huawei cloud: creating a new solution for smart teaching in Africa
Flutter 2.0 FocusScope. of(context). The requestfocus (focusnode()) does not take effect
物理验证LVS流程和技术点滴(上)
Win11 system component cannot be opened? Win11 system widget cannot be opened solution