AC java code with two arraylist,but slow about 600ms


  • 0
    N
    class MinStack {
        List<Integer> stack = new ArrayList<Integer>();
        List<Integer> min = new ArrayList<Integer>();
        int m = Integer.MAX_VALUE;
        public void push(int x) {
            stack.add(0,x);
            if(min.isEmpty() || x< m)
            {
                m = x;
            }
            min.add(0,m);
        }
    
        public void pop() {
            stack.remove(0);
            min.remove(0);
            if(!min.isEmpty())
                m = min.get(0);
            else
                m = Integer.MAX_VALUE;
        }
    
        public int top() {
           return stack.get(0);
        }
    
        public int getMin() {
          
            return min.get(0);
        }
    }
    

    any way to be faster?


Log in to reply
 

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