当前位置:网站首页>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(); */
边栏推荐
- The inconsistency between the versions of dynamic library and static library will lead to bugs
- ADS-NPU芯片架构设计的五大挑战
- vSphere实现虚拟机迁移
- SCM Chinese data distribution
- Mobilenet series (5): use pytorch to build mobilenetv3 and learn and train based on migration
- SAP Spartacus home 页面读取 product 数据的请求的 population 逻辑
- The population logic of the request to read product data on the sap Spartacus home page
- cf:D. Insert a Progression【关于数组中的插入 + 绝对值的性质 + 贪心一头一尾最值】
- Recursive method converts ordered array into binary search tree
- 程序员成长第九篇:真实项目中的注意事项
猜你喜欢
VSphere implements virtual machine migration
MobileNet系列(5):使用pytorch搭建MobileNetV3并基于迁移学习训练
Dede collection plug-in free collection release push plug-in
Building core knowledge points
The population logic of the request to read product data on the sap Spartacus home page
cf:H. Maximal AND【位运算练习 + k次操作 + 最大And】
Cf:d. insert a progression [about the insert in the array + the nature of absolute value + greedy top-down]
Recommended areas - ways to explore users' future interests
ADS-NPU芯片架构设计的五大挑战
Opinions on softmax function
随机推荐
How to extract MP3 audio from MP4 video files?
[groovy] compile time meta programming (AST syntax tree conversion with annotations | define annotations and use groovyasttransformationclass to indicate ast conversion interface | ast conversion inte
golang mqtt/stomp/nats/amqp
[groovy] compile time metaprogramming (compile time method injection | method injection using buildfromspec, buildfromstring, buildfromcode)
【第30天】给定一个整数 n ,求它的因数之和
Intensive learning weekly, issue 52: depth cuprl, distspectrl & double deep q-network
Pbootcms plug-in automatically collects fake original free plug-ins
Three methods of script about login and cookies
基於DVWA的文件上傳漏洞測試
SAP Spartacus home 页面读取 product 数据的请求的 population 逻辑
Hundreds of lines of code to implement a JSON parser
VSphere implements virtual machine migration
程序员成长第九篇:真实项目中的注意事项
Mobilenet series (5): use pytorch to build mobilenetv3 and learn and train based on migration
Dedecms plug-in free SEO plug-in summary
GNSS terminology
毕设-基于SSM高校学生社团管理系统
[groovy] XML serialization (use markupbuilder to generate XML data | set XML tag content | set XML tag attributes)
Starting from 1.5, build a micro Service Framework - call chain tracking traceid
Building core knowledge points