当前位置:网站首页>Leetcode sword finger offer 59 - ii Maximum value of queue
Leetcode sword finger offer 59 - ii Maximum value of queue
2022-07-06 01:11:00 【May Hacker】
subject
Please define a queue and implement the function max_value Get the maximum in the queue , Function required max_value、push_back and pop_front The average time complexity of is O(1).
If the queue is empty ,pop_front and max_value Need to return -1
Example 1:
Input :
["MaxQueue","push_back","push_back","max_value","pop_front","max_value"]
[[],[1],[2],[],[],[]]
Output : [null,null,null,2,1,2]
Example 2:
Input :
["MaxQueue","pop_front","max_value"]
[[],[],[]]
Output : [null,-1,-1]
Limit :
1 <= push_back,pop_front,max_value Total number of operations for <= 10000
1 <= value <= 10^5
source : Power button (LeetCode)
link :https://leetcode.cn/problems/dui-lie-de-zui-da-zhi-lcof
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas
- Pay special attention if Java Be familiar with this topic Deque and Queue Of API, about Deque Come on , It can be a queue or a stack , This topic will be used as a queue , So to view the tail element, you should use PeekLast(), instead of Peek(), because Peek() amount to PeekFirst()
- Maintain an additional two-way queue that holds the maximum value , From head to tail , It should be in reverse order , That is, when the element at the end of the team is smaller than the element currently entering the team , want while Pop up these small elements , So as to ensure that it is in reverse order
- When you push the stack , Just judge whether the heads of the two queues are equal , If it is equal, the reverse two-way queue will also pop up .
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(); */
边栏推荐
- Paging of a scratch (page turning processing)
- WGet: command line download tool
- MobileNet系列(5):使用pytorch搭建MobileNetV3并基于迁移学习训练
- GNSS terminology
- Cannot resolve symbol error
- VMware Tools安装报错:无法自动安装VSock驱动程序
- 2020.2.13
- Mobilenet series (5): use pytorch to build mobilenetv3 and learn and train based on migration
- BiShe - College Student Association Management System Based on SSM
- 记一个 @nestjs/typeorm^8.1.4 版本不能获取.env选项问题
猜你喜欢
Building core knowledge points
Daily practice - February 13, 2022
Recommended areas - ways to explore users' future interests
MobileNet系列(5):使用pytorch搭建MobileNetV3并基于迁移学习训练
cf:D. Insert a Progression【关于数组中的插入 + 绝对值的性质 + 贪心一头一尾最值】
从 1.5 开始搭建一个微服务框架——调用链追踪 traceId
可恢复保险丝特性测试
Starting from 1.5, build a micro Service Framework - call chain tracking traceid
1791. Find the central node of the star diagram / 1790 Can two strings be equal by performing string exchange only once
For a deadline, the IT fellow graduated from Tsinghua suddenly died on the toilet
随机推荐
golang mqtt/stomp/nats/amqp
Arduino hexapod robot
关于#数据库#的问题:(5)查询库存表中每本书的条码、位置和借阅的读者编号
The population logic of the request to read product data on the sap Spartacus home page
在产业互联网时代,将会凭借大的产业范畴,实现足够多的发展
Keepalive component cache does not take effect
【第30天】给定一个整数 n ,求它的因数之和
Cf:c. the third problem
Dede collection plug-in free collection release push plug-in
[groovy] JSON serialization (jsonbuilder builder | generates JSON string with root node name | generates JSON string without root node name)
In the era of industrial Internet, we will achieve enough development by relying on large industrial categories
Differences between standard library functions and operators
Leetcode study - day 35
Hundreds of lines of code to implement a JSON parser
Daily practice - February 13, 2022
几百行代码实现一个 JSON 解析器
Why can't mathematics give machine consciousness
KDD 2022 | EEG AI helps diagnose epilepsy
Ubantu check cudnn and CUDA versions
[groovy] JSON serialization (convert class objects to JSON strings | convert using jsonbuilder | convert using jsonoutput | format JSON strings for output)