当前位置:网站首页>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(); */
边栏推荐
- Gartner发布2022-2023年八大网络安全趋势预测,零信任是起点,法规覆盖更广
- Cf:c. the third problem
- Recursive method converts ordered array into binary search tree
- After Luke zettlemoyer, head of meta AI Seattle research | trillion parameters, will the large model continue to grow?
- Blue Bridge Cup embedded stm32g431 - the real topic and code of the eighth provincial competition
- golang mqtt/stomp/nats/amqp
- GNSS terminology
- Zhuhai laboratory ventilation system construction and installation instructions
- Use of crawler manual 02 requests
- BiShe - College Student Association Management System Based on SSM
猜你喜欢

基于DVWA的文件上传漏洞测试

看抖音直播Beyond演唱会有感

Daily practice - February 13, 2022

Unity | 实现面部驱动的两种方式
![[groovy] JSON string deserialization (use jsonslurper to deserialize JSON strings | construct related classes according to the map set)](/img/bf/18ef41a8f30523b7ce57d03f93892f.jpg)
[groovy] JSON string deserialization (use jsonslurper to deserialize JSON strings | construct related classes according to the map set)

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

Who knows how to modify the data type accuracy of the columns in the database table of Damon

Some features of ECMAScript

Dedecms plug-in free SEO plug-in summary
![Cf:h. maximum and [bit operation practice + K operations + maximum and]](/img/c2/9e58f18eec2ff92e164d8d156629cf.png)
Cf:h. maximum and [bit operation practice + K operations + maximum and]
随机推荐
The detailed page returns to the list and retains the original position of the scroll bar
从 1.5 开始搭建一个微服务框架——调用链追踪 traceId
Is chaozhaojin safe? Will it lose its principal
IP storage and query in MySQL
Overview of Zhuhai purification laboratory construction details
vSphere实现虚拟机迁移
Some features of ECMAScript
KDD 2022 | EEG AI helps diagnose epilepsy
在产业互联网时代,将会凭借大的产业范畴,实现足够多的发展
Beginner redis
Introduction to robotics I. spatial transformation (1) posture, transformation
小程序容器可以发挥的价值
cf:D. Insert a Progression【关于数组中的插入 + 绝对值的性质 + 贪心一头一尾最值】
FFT learning notes (I think it is detailed)
[pat (basic level) practice] - [simple mathematics] 1062 simplest fraction
MCU realizes OTA online upgrade process through UART
CTF daily question day44 rot
孤勇者
毕设-基于SSM高校学生社团管理系统
Finding the nearest common ancestor of binary search tree by recursion