当前位置:网站首页>剑指 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;
}
}
边栏推荐
- 福昕软件受邀亮相2022先进制造业数智发展论坛
- 【Proteus仿真】矩阵键盘中断扫描
- Static static member variables use @value injection
- 技术保证质量,软件测试的这些测试方法你都掌握了吗?
- 深度好文 | YOLOv5+DeepSORT多目标跟踪深入解读与测试(含源码)
- Have you mastered all the testing methods of technology to ensure quality and software testing?
- Nacos problem
- jfinal中如何使用过滤器监控Druid监听SQL执行?
- WPS and Excelle
- JVM(2) 垃圾回收
猜你喜欢
KDD 2022 | 协同过滤中考虑表征对齐和均匀性
学习放大器至少要3年?
开发者任务中心上线!千元豪礼送不停!
QC协议+华为FCP+三星AFC快充取电5V9V芯片FS2601应用
Game Maker 基金会呈献:归属之谷
C语言数组专题训练
3-3 host discovery - layer 4 discovery
Kdd 2022 | prise en compte de l'alignement et de l'uniformité des représentations dans le Filtrage collaboratif
Common knowledge of ECS security settings
深度好文 | YOLOv5+DeepSORT多目标跟踪深入解读与测试(含源码)
随机推荐
Canonical的工程师们正努力解决Firefox Snap的性能问题
MBA-day19 如果p则q矛盾关系p 且非q
Exploration and practice of NLP problem modeling scheme
以其他组件为代价的性能提升不是好提升
npm ERR! fatal: early EOF npm ERR! fatal: index-pack failed
物理验证LVS流程和技术点滴(上)
npm ERR! fatal: early EOF npm ERR! fatal: index-pack failed
k线图经典图解(收藏版)
Flutter 调用百度地图APP实现位置搜索、路线规划
After CDN is added to the website, the Font Icon reports an error access control allow origin
MSYQL, redis, mongodb visual monitoring tool grafana
14.04 million! Sichuan provincial human resources and social security department relational database and middleware software system upgrade procurement bidding!
WPS和Excele
打新债线上开户安全吗
【剑指Offer】51. 数组中的逆序对
What about frequent network disconnection of win11 system? Solution to win11 network instability
细说GaussDB(DWS)复杂多样的资源负载管理手段
nacos 问题
创作者基金会 6 月份亮点
QC protocol + Huawei fcp+ Samsung AFC fast charging 5v9v chip fs2601 application