Reason for Output Limit Exceeded?


  • 0
    J

    The following is my C++ code. While I am not sure if it is working correctly, I don't understand why I kept getting Output Limit Exceeded. Any suggestions would be appreciated. Many thanks!

    class Solution {
    public:
        int removeDuplicates(int A[], int n) {
            if (n == 0) return 0;
        	int i = 1, j = 1;
    		while (i < n) {
    			if (A[i] == A[i - 1]) {
    				j = i;
    				while (A[j] == A[i] && j < n) j++;
    				if (j < n) {
    				    A[i] = A[j];
    				} else {
    				    break;
    				}
    			}
    			i++;
    		}
    		return i;
        }
    };

  • 0
    B

    Well, what if the input is such that the code never goes inside "if (A[i] == A[i-1])"? Not only you have an infinite loop, you will also have ABR.


  • 0
    J

    That's a great point! Thanks for pointing it out! But I still get Output Limit Exceeded even after I added the condition i < n in the while loop... I have edited the code in the question to be clear.


Log in to reply
 

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