当前位置:网站首页>Leetcode 剑指 Offer 59 - II. 队列的最大值
Leetcode 剑指 Offer 59 - II. 队列的最大值
2022-07-06 01:10:00 【May Hacker】
题目
请定义一个队列并实现函数 max_value 得到队列里的最大值,要求函数max_value、push_back 和 pop_front 的均摊时间复杂度都是O(1)。
若队列为空,pop_front 和 max_value 需要返回 -1
示例 1:
输入:
["MaxQueue","push_back","push_back","max_value","pop_front","max_value"]
[[],[1],[2],[],[],[]]
输出: [null,null,null,2,1,2]
示例 2:
输入:
["MaxQueue","pop_front","max_value"]
[[],[],[]]
输出: [null,-1,-1]
限制:
1 <= push_back,pop_front,max_value的总操作数 <= 10000
1 <= value <= 10^5
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/dui-lie-de-zui-da-zhi-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路
- 特别注意如果用Java做本题要熟悉Deque和Queue的API,对于Deque来说,能当队列也能当栈,本题会当队列来用,所以查看尾部元素应该用PeekLast(),而不是Peek(),因为Peek()相当于PeekFirst()
- 额外维护一个保存最大值的双向队列,从队头到队尾,应该是逆序的,即当队尾元素小于当前要进队的元素时,要while弹出去这些小元素,从而保证是逆序的
- 出栈的时候,只需要判断一下两个队列的队头是否相等,相等则把这个逆序双向队列也弹出。
Java AC
class MaxQueue {
private Queue<Integer> q = new LinkedList<>();
private Deque<Integer> dq = new LinkedList<>();
public MaxQueue() {
}
public int max_value() {
if(q.isEmpty()){
return -1;
}
return dq.peekFirst();
}
public void push_back(int value) {
q.add(value);
while(!dq.isEmpty()&& dq.peekLast()<value){
dq.removeLast();
}
dq.add(value);
}
public int pop_front() {
if(q.isEmpty()){
return -1;
}
int res = q.poll();
if(dq.peekFirst()==res){
dq.removeFirst();
}
return res;
}
}
/** * Your MaxQueue object will be instantiated and called as such: * MaxQueue obj = new MaxQueue(); * int param_1 = obj.max_value(); * obj.push_back(value); * int param_3 = obj.pop_front(); */
边栏推荐
- 小程序容器可以发挥的价值
- GNSS terminology
- The population logic of the request to read product data on the sap Spartacus home page
- 【第30天】给定一个整数 n ,求它的因数之和
- esxi的安装和使用
- Finding the nearest common ancestor of binary tree by recursion
- 看抖音直播Beyond演唱会有感
- Finding the nearest common ancestor of binary search tree by recursion
- 新手入门深度学习 | 3-6:优化器optimizers
- Starting from 1.5, build a micro Service Framework - call chain tracking traceid
猜你喜欢

Dedecms plug-in free SEO plug-in summary

95后CV工程师晒出工资单,狠补了这个,真香...

Some features of ECMAScript

程序员搞开源,读什么书最合适?

vSphere实现虚拟机迁移

Starting from 1.5, build a micro Service Framework - call chain tracking traceid

Leetcode study - day 35

cf:H. Maximal AND【位运算练习 + k次操作 + 最大And】

Illustrated network: the principle behind TCP three-time handshake, why can't two-time handshake?

测试/开发程序员的成长路线,全局思考问题的问题......
随机推荐
Four dimensional matrix, flip (including mirror image), rotation, world coordinates and local coordinates
BiShe - College Student Association Management System Based on SSM
View class diagram in idea
看抖音直播Beyond演唱会有感
ThreeDPoseTracker项目解析
golang mqtt/stomp/nats/amqp
[groovy] XML serialization (use markupbuilder to generate XML data | create sub tags under tag closures | use markupbuilderhelper to add XML comments)
Gartner发布2022-2023年八大网络安全趋势预测,零信任是起点,法规覆盖更广
Keepalive component cache does not take effect
在产业互联网时代,将会凭借大的产业范畴,实现足够多的发展
Arduino hexapod robot
Xunrui CMS plug-in automatically collects fake original free plug-ins
[pat (basic level) practice] - [simple mathematics] 1062 simplest fraction
In the era of industrial Internet, we will achieve enough development by relying on large industrial categories
MYSQL---查询成绩为前5名的学生
图解网络:TCP三次握手背后的原理,为啥两次握手不可以?
Kotlin core programming - algebraic data types and pattern matching (3)
程序员成长第九篇:真实项目中的注意事项
关于#数据库#的问题:(5)查询库存表中每本书的条码、位置和借阅的读者编号
Programmer growth Chapter 9: precautions in real projects