3. (155) minimum stack

2020 March 21

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.

A support designed to push, pop, top operation, the stack can be retrieved and smallest elements in constant time.

  • push (x) - the element x push the stack.
  • pop () - delete elements 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();   --> 返回 -3.
minStack.pop();
minStack.top();      --> 返回 0.
minStack.getMin();   --> 返回 -2.

method 1

The most direct method is to use two stacks, a stack of the normal value stored in the stack, the stack to keep the other minimum is the minimum of all save the current element with the current top of the stack, the process is as follows:

  1. The first element of the stack
  2. If the newly added element is greater than the top of the stack, then the newly added elements will not deal with
  3. If the newly added element is less than equal to the top element, the new element onto the stack
  4. Stack of elements is not equal to the top element is not operated
  5. The stack element is equal to the top element, the top element from the stack
class MinStack{
    private Stack<Integer> stack;
    private Stack<Integer> minStack;
    
    public MinStack(){
        stack=new Stack<>();
        minStack=new Stack<>();
    }
    
    public void push(int x){
        stack.push(x);
        if(!minStack.isEmpty()){
            int top=minStack.peek();
            //小于等于的时候才入栈
            if(x<=top){
                minStack.push(x);
            }
        }else{
            minStack.push(x);
        }
    }
    
    public void pop(){
        int pop=stack.pop();
        
        int top=minStack.peek();
        //等于的时候再出栈
        if(pop==top){
            minStack.pop();
        }
    }
    
    public int top(){
        return stack.peek();
    }
    
    public int getMin(){
        return minStack.peek();
    }
    
    public int getMin(){
        return minStack.peek();
    }
}

Method 2

The method uses a two stack implementation, now try to achieve a stack, only one variable to save the minimum value, if the new value is smaller pushed, then before the new element is pressed into the original onto the stack min and updates the minimum value

class minStack{
    int min = Integer.MIN_VALUE;
    Stack<Integer> stack = new Stack<Integer>();
    public void push(int x){
        if(x<=min){
            stack.push(min);
            min=x;
        }
        stack.push(x);
    }
    
    public void pop(){
        if(stack.pop()==min){
            min = stack.pop();
        }
    }
    
    public int top(){
        return stack.peek();
    }
    
    public int getMin(){
        return min;
    }
}

Method 3

As can be seen from the method, the key problem to be solved when there is a new smaller value, the minimum value before how to do?

Method 3 with another idea, the minimum difference between the current value and the minimum value stored in the stack by the stack memory min

public class MinStack{
    long min;
    Stack<long> stack;
    
    public MinStack(){
        stack=new Stack<>();
    }
    
    public void push(int x){
        if(stack.isEmpty()){
            min=x;
            stack.push(x-min);
        }else{
            stack.push(x-min);
            if(x<min){
                min=x;
            }
        }
    }
    
    public void pop(){
        if(stack.isEmpty()) return;
        long pop = stack.pop();
        if(pop<0){
            min=min-pop;
        }
    }
    
    public int top(){
        long top=stack.peek();
        if(top<0){
            return (int)(min);
        }else{
            return (int)(top+min);
        }
    }
    
    
    public int getMin(){
        return (int) min;
    }
}

One disadvantage of the above solution is saved is the difference, it may cause overflow, so use a wider range of long type, this update does not require a minimum solution when the minimum deposit before it will save some space

Method 4

java stack not provided, min increase in junction field Node

class MinStack{
    class Node{
        int value;
        int min;
        Node next;
        
        Node(int x,int min){
            this.value=x;
            this.min=min;
            next=null;
        }
    }
    Node head;
    
    public void push(int x){
        if(null==head){
            head=new Node(x,x);
        }else{
            Node n = new Node(x,Math.min(x,head.min))
            n.next=head;
            head=n;
        }
    }
    
    public void pop(){
        if(head!=null)
            head=head.next;
    }
    
    public int top(){
        if(head!=null)
            return head.value;
        return -1;
    }
    
    public int getMin(){
        if(head!=null)
            return head.min;
        return -1;
    }
}

Guess you like

Origin www.cnblogs.com/ningdeblog/p/12542013.html