Leetcode:剑指 Offer 59 - II. 队列的最大值
双端队列queue为主队列,寄存全副元素,help用于寄存主队列的最大值。

1.入队:

queue:把元素value直接插入queue后即可。
help:把help前面的小于value用help.pollLast()删除,再把value用help.addLast()退出。

2.出队:

queue:用queue.pollFirst()把元素出队。
help:如果pollFirst()的元素等于help的左端头元素的值,用help.pollFirst()把元素出队。

class MaxQueue {    LinkedList<Integer> queue;    LinkedList<Integer> help;    public MaxQueue() {        queue = new LinkedList<Integer>();        help = new LinkedList<Integer>();    }        public int max_value() {       if(help.isEmpty()) return -1;       return help.peekFirst();    }        public void push_back(int value) {        queue.addLast(value);        while(!help.isEmpty() && value > help.peekLast()){            help.pollLast();        }        help.addLast(value);    }        public int pop_front() {        if(queue.isEmpty()) return -1;        int value = queue.pollFirst();        if(value == help.peekFirst()){            help.pollFirst();        }        return value;    }}