
- 因为要在常量时间内查询出最小值,所以需要有另外的数据结构维护最小值,很自然地想到了“堆”这个结构,“最小堆”的堆顶元素刚好是最小值
 - 因此出栈和入栈的同时也要维护好最小堆
 
class MinStack {
    PriorityQueue<Integer> heap;
    LinkedList<Integer> stack;
    public MinStack() {
        this.heap=new PriorityQueue<>();
        this.stack=new LinkedList<>();
    }
    public void push(int val) { // 入栈
        heap.offer(val);
        stack.offer(val);
    }
    public void pop() { // 出栈
        Integer peek = stack.pollLast();
        heap.remove(peek);
    }
    public int top() {
        return stack.getLast();
    }
    public int getMin() {
        return heap.peek();
    }
}
/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(val);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */
                

















