class MaxQueue {
public MaxQueue() {
}
Queue<Integer> queue = new LinkedList<>();
LinkedList<Integer> max_list = new LinkedList<>();
public int max_value() {
if (max_list.isEmpty()) {
return -1;
}
else {
return max_list.peekFirst();
}
}
public void push_back(int value) {
while (!max_list.isEmpty() && max_list.peekLast() < value) {
max_list.pollLast();
}
queue.add(value);
max_list.add(value);
}
public int pop_front() {
if (queue.isEmpty()) {
return -1;
}
else {
int value = queue.poll();
if (value == max_list.peekFirst()) max_list.pollFirst();
return value;
}
}
}
/**
* 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();
*/
08-09