Easy AC Java solution


  • 0
    G
        List<Integer> stack; 
        int min=Integer.MAX_VALUE;//store the min value
    
        public MinStack() {
            stack=new ArrayList<Integer>();
        }
        public void push(int x) {
        	stack.add(x);
        	min=min>x ? x : min;
        }
        
        public void pop() {
        	if(stack.get(stack.size()-1)==min){
            	min=Integer.MAX_VALUE;
    	        for(int i=0;i<stack.size()-1;i++){
    	        	if(min>stack.get(i)){
    	        		min=stack.get(i);
    	        	}
    	        }
        	}
            stack.remove(stack.size()-1);
        }
        
        public int top() {
        	return stack.get(stack.size()-1);  
        }
       
        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.