155. Min Stack

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.

Solution 1: use an arrayList to implement the min stack, here comes problem: how toget min element, use global min to tag minimal element and then scan the rest part when the min is pop out : O(n) time

class MinStack {
    //list : get size ,remove
    /** initialize your data structure here. */
    List<Integer> stack ;
    int min ;
    int size ;
    public MinStack() {
        stack = new ArrayList<>();
        min = Integer.MAX_VALUE;
        size = 0;
    }
    public void updateMin(){
        min = Integer.MAX_VALUE;
        for(int i = 0; i<size-1; i++){
            if(stack.get(i) < min) min = stack.get(i);
        }
        
    }
    public void push(int x) {
        stack.add(x);
        size++;
        if(x < min) min = x;
    }
    
    public void pop() {
        if(!stack.isEmpty()){
            int pop = stack.get(size - 1);
            if(min == pop){
                updateMin();
            }
            stack.remove(size - 1);
            size -- ;
        }
    }
    
    public int top() {
        return stack.get(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();
 */

 

Solution 2: use one array and one stack(maintain  the min element(including the second min element)) to implement the Min stack

how to use min stack make sure the second element 6 3 are in stack, if we have 4, it does not matter. reference : https://blog.csdn.net/linhuanmars/article/details/41008731

CAution: check the stack empty and >= (duplicate element)

class MinStack {
    ArrayList<Integer> list;
    ArrayList<Integer> stack ;//keep min element
    /** initialize your data structure here. */
    public MinStack() {
        list = new ArrayList<Integer>();
        stack = new ArrayList<Integer>();
    }
    
    public void push(int x) {
        list.add(x);
        if(stack.isEmpty() || stack.get(stack.size()-1) >= x) stack.add(x);// duplicate element
    }
    
    public void pop() {
        
        if(!list.isEmpty()){
            int ele = list.get(list.size() - 1 );
            list.remove(list.size()-1);
            if(!stack.isEmpty() && ele == stack.get(stack.size() - 1)){
                stack.remove(stack.size()-1);
            }
        }
    }
    
    public int top() {
        return list.get(list.size() - 1 );
    }
    
    public int getMin() {
        if(!stack.isEmpty()){
            return stack.get(stack.size() - 1);
        }
        return 0; // it is empty
    }
}

/**
 * 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();
 */

 

posted @ 2018-08-08 03:22  wz30  阅读(108)  评论(0编辑  收藏  举报