当前位置:网站首页>剑指 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;
}
}
边栏推荐
- 7. cancellation and closing
- AI scene Storage Optimization: yunzhisheng supercomputing platform storage practice based on juicefs
- 14,04 millions! Appel d'offres pour la mise à niveau de la base de données relationnelle et du système logiciel Middleware du Département des ressources humaines et sociales de la province du Sichuan!
- Shell bash script note: there must be no other irrelevant characters after the escape character \ at the end of a single line (multi line command)
- 【历史上的今天】6 月 29 日:SGI 和 MIPS 合并;微软收购 PowerPoint 开发商;新闻集团出售 Myspace
- 【U盘检测】为了转移压箱底的资料,买了个2T U盘检测仅仅只有47G~
- 3-3主機發現-四層發現
- 【精品】pinia详解
- 凌云出海记 | 文华在线&华为云:打造非洲智慧教学新方案
- 一个mysql里有3306端口下,一个mysql有20多个数据库,怎么一键备份20多个数据库,做系统备份,防止数据误删除?
猜你喜欢

QC protocol + Huawei fcp+ Samsung AFC fast charging 5v9v chip fs2601 application

深度好文 | YOLOv5+DeepSORT多目标跟踪深入解读与测试(含源码)

Shell bash script note: there must be no other irrelevant characters after the escape character \ at the end of a single line (multi line command)

学习放大器至少要3年?

@SneakyThrows注解

云服务器的安全设置常识

细说GaussDB(DWS)复杂多样的资源负载管理手段

Creators foundation highlights in June

How to install and use computer SSD hard disk
![[笔记]再笔记--边干边学Verilog HDL –008](/img/7f/0ca73446247455ac4d8f9667083a87.png)
[笔记]再笔记--边干边学Verilog HDL –008
随机推荐
ovirt数据库修改删除节点
With these four security testing tools, software security testing can be said so easy!
QC协议+华为FCP+三星AFC快充取电5V9V芯片FS2601应用
Wechat launched the picture big bang function; Apple's self-developed 5g chip may have failed; Microsoft solves the bug that causes edge to stop responding | geek headlines
软件工程专业大二,之前的学习情况不太好该怎么规划后续发展路线
Game Maker 基金会呈献:归属之谷
[proteus simulation] matrix keyboard interrupt scanning
Classic illustration of K-line diagram (Collection Edition)
JVM (4) bytecode technology + runtime optimization
Win11策略服务被禁用怎么办?Win11策略服务被禁用的解决方法
Creators foundation highlights in June
4-1端口扫描技术
开发者任务中心上线!千元豪礼送不停!
AI scene Storage Optimization: yunzhisheng supercomputing platform storage practice based on juicefs
出逃与进军,临期食品的「双面江湖」
JVM(3) 类加载
14,04 millions! Appel d'offres pour la mise à niveau de la base de données relationnelle et du système logiciel Middleware du Département des ressources humaines et sociales de la province du Sichuan!
[笔记]再笔记--边干边学Verilog HDL –008
JVM (3) class loading
ArrayList&lt;Integer&gt;使用==比较值是否相等出现 -129!=-129的情况思考