Why is it exceeding runtime for elimination game?


  • 0
    D

    Hi ,

    I ran this code on Eclipse IDE and it is passing the test cases, however on LeetCode it is saying it exceeds Runtime.

    If possible, can the admin, see my code and make a decision

    public class Solution {
    public int lastRemaining(int n) {

        //Base case : if number is less than 2, return number itself
        if(n<2)
            return n;
       
        //General case: if n is greater than 1
        
        //arraylist t store list of numbers    
        ArrayList<Integer> list=new ArrayList<Integer>();
        
        for(int i=0;i<n;i++)
            list.add(i+1);
    
        //delete all characters until only 1 char is left
        while(list.size()!=1)   
        {
            
            for(int i=0;i<list.size();i++)
                list.remove(i);
            
            //if only 1 number remains, return that answer
            if(list.size()==1)
                break;
                
            for(int i=list.size()-1;i>=0;i-=2)
                list.remove(i);
        }
        
        
        return list.get(0);
                
                
            
        
        
        
    }
    

    }


Log in to reply
 

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