当前位置:网站首页>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(); */
边栏推荐
- The basic usage of JMeter BeanShell. The following syntax can only be used in BeanShell
- 朝招金安全吗 会不会亏损本金
- 几百行代码实现一个 JSON 解析器
- Novice entry depth learning | 3-6: optimizer optimizers
- Cf:c. the third problem
- Paging of a scratch (page turning processing)
- curlpost-php
- [day 30] given an integer n, find the sum of its factors
- Promise
- Distributed base theory
猜你喜欢
[groovy] compile time meta programming (AST syntax tree conversion with annotations | define annotations and use groovyasttransformationclass to indicate ast conversion interface | ast conversion inte
Daily practice - February 13, 2022
Study diary: February 13, 2022
MobileNet系列(5):使用pytorch搭建MobileNetV3并基于迁移学习训练
Recoverable fuse characteristic test
Opinions on softmax function
Fibonacci number
Installation and use of esxi
JVM_ 15_ Concepts related to garbage collection
程序员搞开源,读什么书最合适?
随机推荐
程序员成长第九篇:真实项目中的注意事项
Convert binary search tree into cumulative tree (reverse middle order traversal)
Recoverable fuse characteristic test
基於DVWA的文件上傳漏洞測試
curlpost-php
The growth path of test / development programmers, the problem of thinking about the overall situation
[groovy] compile time metaprogramming (compile time method interception | find the method to be intercepted in the myasttransformation visit method)
logstash清除sincedb_path上传记录,重传日志数据
95后CV工程师晒出工资单,狠补了这个,真香...
Who knows how to modify the data type accuracy of the columns in the database table of Damon
Development trend of Ali Taobao fine sorting model
Obstacle detection
程序员搞开源,读什么书最合适?
Illustrated network: the principle behind TCP three-time handshake, why can't two-time handshake?
Finding the nearest common ancestor of binary tree by recursion
VMware Tools安装报错:无法自动安装VSock驱动程序
Hcip---ipv6 experiment
SCM Chinese data distribution
A preliminary study of geojson
[groovy] JSON string deserialization (use jsonslurper to deserialize JSON strings | construct related classes according to the map set)