【解题思路】
刚开始用了优先队列PriorityQueue,在一组测试样例中过不了,输出总是[], [], [], [], [], [], [], 16, [], 22,正确答案应该是[], [], [], [], [], [], [], 96, [], 16。经过调试,终于想起了,这是优先队列,默认将队列按照升序排列,而此题应该使用普通队列。
class MaxQueue {
int max = -99999;
Queue<Integer> queue;
public MaxQueue() {
queue = new LinkedList<Integer>();
}
public int max_value() {
if(queue.isEmpty())
{
return -1;
}
return max;
}
public void push_back(int value) {
if(value > max)
{
max = value;
}
queue.offer(value);
}
public int pop_front() {
if(queue.isEmpty())
{
return -1;
}
int num = queue.poll();
if(num == max)
{
max = -99999;
Queue<Integer> q = new LinkedList<Integer>();
while(!queue.isEmpty())
{
int tmp = queue.peek();
if(tmp > max) max = tmp;
q.offer(queue.poll());
}
while(!q.isEmpty())
{
queue.offer(q.poll());
}
}
return num;
}
}
/**
* 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();
*/