Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

  • push(x) -- Push element x onto stack.
  • pop() -- Removes the element on top of the stack.
  • top() -- Get the top element.
  • getMin() -- Retrieve the minimum element in the stack.

 

Example:

MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin();   --> Returns -3.
minStack.pop();
minStack.top();      --> Returns 0.
minStack.getMin();   --> Returns -2.


设计一个可以获得最小值的栈。

 

第一次用了ArrayList和Stack,超时了。

public class MinStack {

    Stack<Integer> stack ;
    ArrayList<Integer> list;
    public MinStack() {
        stack = new Stack<Integer>();
        list = new ArrayList<Integer>();
    }

    public void push(int x) {
        stack.push(x);
        int flag = 0;
        if( list.size() == 0 ){
            list.add(x);
            return ;
        }
        for( int i = 0 ; i < list.size() && flag == 0;i++){
            if( list.get(i) > x ){
                list.add(i,x);
                flag = 1;
            }
        }
        if( flag == 0)
            list.add(list.size(),x);
        

    }

    public void pop() {

        list.remove((Integer) stack.pop());
    }

    public int top() {
        return stack.peek();
    }

    public int getMin() {
        return list.get(0);
    }
}

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(x);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */

 

只使用一个ArrayList

public class MinStack {

    ArrayList<Integer> list;
    int min ;
    public MinStack() {
        list = new ArrayList<Integer>();
    }

    public void push(int x) {
        list.add(x);
        if( list.size() == 1)
            min = x;
        else
            min = Math.min(min,x);
    }

    public void pop() {
        list.remove( list.size()-1 );
        if( list.size() == 0)
            return ;
        min = list.get(0);
        for( int i = 1 ; i < list.size();i++)
            min = Math.min(min,list.get(i));

    }

    public int top() {
        return list.get(list.size()-1);
    }

    public int getMin() {
        return min;
    }
}

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(x);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */

 

3、使用两个栈实现。

 

public class MinStack {

    private Stack<Integer> stack = new Stack<Integer>();  
    // minStack: store the current min values  
    private Stack<Integer> minStack = new Stack<Integer>();  
  
    public void push(int x) {  
        // store current min value into minStack  
        if (minStack.isEmpty() || x <= minStack.peek())  
            minStack.push(x);  
        stack.push(x);  
    }  
  
    public void pop() {  
        // use equals to compare the value of two object, if equal, pop both of them  
        if (stack.peek().equals(minStack.peek()))  
            minStack.pop();  
        stack.pop();  
    }  
  
    public int top() {  
        return stack.peek();  
    }  
  
    public int getMin() {  
        return minStack.peek();  
    }
}

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(x);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */

 

 

4、也可以只用一个栈实现。

public class MinStack {
    long min;
    Stack<Long> stack;

    public MinStack(){
        stack=new Stack<>();
    }
    
    public void push(int x) {
        if (stack.isEmpty()){
            stack.push(0L);
            min=x;
        }else{
            stack.push(x-min);//Could be negative if min value needs to change
            if (x<min) min=x;
        }
    }

    public void pop() {
        if (stack.isEmpty()) return;
        
        long pop=stack.pop();
        
        if (pop<0)  min=min-pop;//If negative, increase the min value
        
    }

    public int top() {
        long top=stack.peek();
        if (top>0){
            return (int)(top+min);
        }else{
           return (int)(min);
        }
    }

    public int getMin() {
        return (int)min;
    }
}

 

 

 

 

 

5、不用栈实现。

class MinStack {
    private Node head;
    
    public void push(int x) {
        if(head == null) 
            head = new Node(x, x);
        else 
            head = new Node(x, Math.min(x, head.min), head);
    }

    public void pop() {
        head = head.next;
    }

    public int top() {
        return head.val;
    }

    public int getMin() {
        return head.min;
    }
    
    private class Node {
        int val;
        int min;
        Node next;
        
        private Node(int val, int min) {
            this(val, min, null);
        }
        
        private Node(int val, int min, Node next) {
            this.val = val;
            this.min = min;
            this.next = next;
        }
    }
}