题目
题目来源:力扣(LeetCode)
请定义一个队列并实现函数 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]
思路分析
从队列尾部插入元素时,我们可以提前取出队列中所有比这个元素小的元素,使得队列中只保留对结 果有影响的数字。这样的方法等价于要求维持队列单调递减,即要保证每个元素的前面都没有比它小的元素。
我们只需要在插入每一个元素 value 时,从队列尾部依次取出比当前元素 value 小的元素,直到遇到 一个比当前元素大的元素 value’ 即可。
上面的过程需要从队列尾部取出元素,因此需要使用双端队列来实现。另外我们也需要一个辅助队列 来记录所有被插入的值,以确定 pop_front 函数的返回值。
保证了队列单调递减后,求最大值时只需要直接取双端队列中的第一项即可。
var MaxQueue = function() {// 记录所有被插入的值this.queue1 = [];// 维护一个值始终递减的队列this.queue2 = [];};/*** @return {number}*/MaxQueue.prototype.max_value = function() {if (this.queue2.length) {// 维护了一个递减的队列,队首元素就是队列中最大的元素return this.queue2[0];}return -1;};/*** @param {number} value* @return {void}*/MaxQueue.prototype.push_back = function(value) {this.queue1.push(value);// 在插入每一个元素 value 时,从队列尾部依次取出比当前元素 value 小的元素,直到遇到一个比当前元素value大的元素while (this.queue2.length && this.queue2[this.queue2.length - 1] < value) {this.queue2.pop();}this.queue2.push(value);};/*** @return {number}*/MaxQueue.prototype.pop_front = function() {if (!this.queue1.length) {return -1;}const value = this.queue1.shift();if (value === this.queue2[0]) {this.queue2.shift();}return value;};/*** 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()*/
