Time limit exceeded: what's wrong with my code?


  • 0

    class Solution
    {
    public:

    int removeDuplicates(int A[], int n)
    {
        int len = n;
    
        for (int i = 1; i < len; i++)
        {       
            while (A[i-1] == A[i])
            {
                    for (int j = i; j < len;j++)
                            A[j-1] = A[j];
                    len--;              
                    if (len < 2)        
                        break;                  
            }           
        }        
        return len;
    
    }
    

    };


  • 0
    P

    Just because your code takes too much iterations to move the values.


  • 0
    B

    The solution asks for a solution with O(A.length) time complexity


Log in to reply
 

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