Code works in Eclipse with cygwin, but OJ denies it...


  • 0
    V

    This is really strange, my code works (for the same input) in Eclipse by giving the correct answer, but OJ says it gives incorrect output.

    The input is: [1,2,2,2,2,0,0,0,1,1]

    The correct output (which is what I get on Eclipse) is: [0,0,0,1,1,1,2,2,2,2]

    Yet OJ say my code gives: [0,0,0,1,2,2,2,2,2,2]

    My code is as follows (approximately one pass with constant space):

    void solution1(int* A, int n) {
        int l = 0, r = n-1, lCount = 0, rCount = 0;
        
        while (l <= r) {
            while (l < n && A[l] == 0) {
                A[l-lCount] = 0;
                l++;
            }
            
            while (r >= 0 && A[r] == 2) {
                A[r+rCount] = 2;
                r--;
            }
            
            if (l <= r && l < n && r >= 0 && A[l] == 1 && A[r] == 0) {
                A[l-lCount] = 0;
                A[r+rCount] = 1;
                l++;
            } else if (l <= r && l < n && r >= 0 && A[l] == 2 && A[r] == 1) {
                A[l-lCount] = 1;
                A[r+rCount] = 2;
                r--;
            } else if (l <= r && l < n && r >= 0 && A[l] == 2 && A[r] == 0) {
                A[l-lCount] = 0;
                A[r+rCount] = 2;
                l++;
                r--;
            } else if (l <= r && l < n && r >= 0 && A[l] == 1 && A[r] == 1) {
                lCount++;
                rCount++;
                l++;
                r--;
            }
        }
        
        for (int i = 0; i < lCount; ++i) {
            A[l-i-1] = 1;
        }
            
        for (int i = 0; i < rCount; ++i) {
            A[r+i+1] = 1;
        }
    }

Log in to reply
 

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