c++
class MaxQueue {
public:
deque<int> que, mq;
MaxQueue() {}
int max_value() {
if (mq.empty()) return -1;
return mq.front();
}
void push_back(int value) {
que.push_back(value);
while (mq.size() && mq.back() < value) mq.pop_back();
mq.push_back(value);
return ;
}
int pop_front() {
if (que.empty()) return -1;
if (que.front() == mq.front()) mq.pop_front();
int ret = que.front();
que.pop_front();
return ret;
}
};
js
var MaxQueue = function() {
this.que = [];
this.mq = [];
};
/**
* @return {number}
*/
MaxQueue.prototype.max_value = function() {
if (!this.mq.length) return -1;
return this.mq[0];
};
/**
* @param {number} value
* @return {void}
*/
MaxQueue.prototype.push_back = function(value) {
this.que.push(value);
while (this.mq.length && this.mq[this.mq.length - 1] < value) this.mq.pop();
this.mq.push(value);
};
/**
* @return {number}
*/
MaxQueue.prototype.pop_front = function() {
if (!this.que.length) return -1;
if (this.que[0] == this.mq[0]) this.mq.shift();
return this.que.shift();
};