Resizing Array


  • 0
    A

    There is a list of sorted integers from 1 to n. Starting from left to right, remove the first number and every other number afterward until you reach the end of the list.
    This was the problem

    Repeat the previous step again, but this time from right to left, remove the right most number and every other number from the remaining numbers.

    We keep repeating the steps again, alternating left to right and right to left, until a single number remains.

    Find the last number that remains starting with a list of length n.

    Input:
    n = 9,
    1 2 3 4 5 6 7 8 9
    2 4 6 8
    2 6
    6
    
    Output:
    6
    

    My Code was this

    import java.util.ArrayList;
    public class Solution {
        public int lastRemaining(int n) {
            int element=1;
            ArrayList<Integer> removeints=new ArrayList<>();
            for(int i=0;i<n;i++)
            {
            	removeints.add(element++);
            }
            boolean flag=true;
            while(flag)
            {
            	if(removeints.size()==1)
                {
                	break;
                }
                for(int i=0;i<removeints.size();i++)
                {
                	removeints.remove(i);
                }
                if(removeints.size()==1)
                {
                	break;
                }
                for(int i=removeints.size()-1;i>=0;i=i-2)
                {
                	if(removeints.size()==1)
                    {
                    	break;
                    }
                	removeints.remove(i);
                }
                if(removeints.size()==1)
                {
                	break;
                }
            }
            return removeints.get(0);
        }
    }
    

    It passed various input tests which I performed both online and on my pc,
    but when I am trying to submit it ,it says "Time Limit Exceeded"
    Has any of you faced a similar issue..!!

    Example:


  • 0
    D

    The solutions you submit here are bound to a time limit. If you exceeded it, it means your program is not efficient enough.
    Even though your code does exactly what the problem states, there are probably more efficient ways of doing it.

    In general it's always a good idea to find the solution that iterates through the list the least amount of times.


  • 0
    A

    @didii : Could you suggest where I can find more solutions for that ..!!


  • 0
    D

    @arpit2408 The whole point of LeetCode is to find your own solutions. If you don't know yet how to solve or think about such problems, try some problems with a solution (where the column "Editorial" has an icon). If you are stuck or don't know how you should do this, you can check the solution to find out how you could solve that problem. Try to understand the solution, why it is a good one, try to learn from the thought process behind it, implement it yourself so you see the details of it (so don't copy-paste) and try another.

    You'll learn!


  • 0
    A

    To Clarify a bit this is my solution and I didn't copy anyone else's code. The only reason I asked this question is because I couldn't find anything why my solution isn't getting expected as I already know that it says its not efficient enough..!!

    So if you can suggest me anything which can help me that would answer my query here..!!


Log in to reply
 

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