大概的思路是对的,但是没有找到合适的数据结构
注意判断弹出值是否相等的时候不能用==,要用equals,但是没想明白为什么
class MaxQueue {
Queue<Integer> q;
Deque<Integer> dq;
public MaxQueue() {
q = new LinkedList<>();
dq = new LinkedList<>();
}
public int max_value() {
if(dq.isEmpty()) return -1;
else return dq.peekFirst();
}
public void push_back(int value) {
q.offer(value);
while(!dq.isEmpty()&&dq.peekLast()<value) dq.removeLast();
dq.addLast(value);
}
public int pop_front() {
if(q.isEmpty()) return -1;
if(q.peek().equals(dq.peekFirst())) dq.removeFirst();
return q.poll();
}
}
/**
* 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();
*/