simple java code - runtimes beats 64.29%


  • 0
    R

    Hello world!

    private LinkedList<Integer> stack;
    private int min;
    
    public MinStack(){
      stack = new LinkedList<>();
      min = Integer.MAX_VALUE;
    }
    
    public void push(int val){
      
       stack.add(val);
       
          if(val < min){
            min = val;
          }
    }
    
    public int top(){
       return stack.getLast();
    }
    
    public int pop(){	
        int last = stack.removeLast();
        maintain(last);
      return last;
    }
    
    public void maintain(int last){
        
        if(last == min){
           	min = Integer.MAX_VALUE;
        } 
    
    	for(int i : stack)
    	     if(i < min){
    	     min = i;
              }
    }
    
    public int getMin(){
      return min;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.