You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
varMaxQueue=function(){this.queue=[];this.max_queue=[];};/** * @return {number} */MaxQueue.prototype.max_value=function(){if(this.queue.length>0){returnthis.max_queue[0];}return-1;};/** * @param {number} value * @return {void} */MaxQueue.prototype.push_back=function(value){this.queue.push(value);if(this.max_queue.length>0){for(leti=this.max_queue.length-1;i>=0;i--){if(this.max_queue[i]<value){this.max_queue.pop();}}}this.max_queue.push(value);};/** * @return {number} */MaxQueue.prototype.pop_front=function(){if(this.queue.length>0){constvalue=this.queue.shift();if(this.max_queue[0]===value){this.max_queue.shift();}returnvalue;}return-1;};/** * Your MaxQueue object will be instantiated and called as such: * var obj = new MaxQueue() * var param_1 = obj.max_value() * obj.push_back(value) * var param_3 = obj.pop_front() */
剑指 Offer 59 - II. 队列的最大值
请定义一个队列并实现函数
max_value
得到队列里的最大值,要求函数max_value
、push_back
和pop_front
的均摊时间复杂度都是 O(1)。若队列为空,
pop_front
和max_value
需要返回 -1Example 1
Example 2
Note
1 <= push_back,pop_front,max_value的总操作数 <= 10000
1 <= value <= 10^5
The text was updated successfully, but these errors were encountered: