当前位置:网站首页>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(); */
边栏推荐
- 95后CV工程师晒出工资单,狠补了这个,真香...
- Xunrui CMS plug-in automatically collects fake original free plug-ins
- Promise
- Differences between standard library functions and operators
- ThreeDPoseTracker项目解析
- View class diagram in idea
- Test de vulnérabilité de téléchargement de fichiers basé sur dvwa
- China Taiwan strategy - Chapter 8: digital marketing assisted by China Taiwan
- [groovy] JSON serialization (jsonbuilder builder | generates JSON string with root node name | generates JSON string without root node name)
- curlpost-php
猜你喜欢
[groovy] XML serialization (use markupbuilder to generate XML data | create sub tags under tag closures | use markupbuilderhelper to add XML comments)
cf:H. Maximal AND【位运算练习 + k次操作 + 最大And】
Questions about database: (5) query the barcode, location and reader number of each book in the inventory table
SAP Spartacus home 页面读取 product 数据的请求的 population 逻辑
2020.2.13
激动人心,2022开放原子全球开源峰会报名火热开启
[groovy] compile time meta programming (compile time method interception | method interception in myasttransformation visit method)
For a deadline, the IT fellow graduated from Tsinghua suddenly died on the toilet
Building core knowledge points
[groovy] JSON serialization (jsonbuilder builder | generates JSON string with root node name | generates JSON string without root node name)
随机推荐
Finding the nearest common ancestor of binary tree by recursion
BiShe - College Student Association Management System Based on SSM
程序员成长第九篇:真实项目中的注意事项
Four dimensional matrix, flip (including mirror image), rotation, world coordinates and local coordinates
View class diagram in idea
[groovy] JSON string deserialization (use jsonslurper to deserialize JSON strings | construct related classes according to the map set)
VSphere implements virtual machine migration
Getting started with devkit
cf:H. Maximal AND【位运算练习 + k次操作 + 最大And】
详细页返回列表保留原来滚动条所在位置
CTF daily question day44 rot
Test de vulnérabilité de téléchargement de fichiers basé sur dvwa
[pat (basic level) practice] - [simple mathematics] 1062 simplest fraction
Leetcode study - day 35
Differences between standard library functions and operators
Dedecms plug-in free SEO plug-in summary
Obstacle detection
Questions about database: (5) query the barcode, location and reader number of each book in the inventory table
Live broadcast system code, custom soft keyboard style: three kinds of switching: letters, numbers and punctuation
Promise