What cause the runtime error?


  • 0
    R

    This is my naive solution to the problem.
    It ran into runtime error with the test case: "push(2147483646),push(2147483646),push(2147483647),top,pop,getMin,pop,getMin,pop,push(2147483647),top,getMin,push(-2147483648),top,getMin,pop,getMin"
    I could not figure out what could have caused it.
    Can anyone help?

    ArrayList<Integer> li = new ArrayList<Integer>();
    int[] min = new int[]{0,Integer.MAX_VALUE};

    public void push(int x) {
        if(x>Integer.MAX_VALUE||x<Integer.MIN_VALUE)
        return;
        li.add(0,x);
        if(x<=min[1]){
            min[0] = 0;
            min[1] = x;
        } else if(x>=min[1]) {
            min[0]++;
        } 
    }
    
    public void pop() {
        int r = li.get(0);
        li.remove(0);
        if(r==min[1]&&min[0]==0){
           min[1] = li.get(0);
           for(int i=1;i<li.size();i++){
               if(li.get(i)<min[1]){
                   min[0] = i;
                   min[1] = li.get(i);
               }
           }
        } else {
            min[0]--;
        }
    }
    
    public int top() {
        return li.get(0);
    }
    
    public int getMin() {
        return min[1];
    

    }


Log in to reply
 

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