Why leetcode give me a wrong answer?


  • 0
    G

    I am trying to solve this program, but I encounter a strange wrong answer .
    when input testcase is
    [[9,0],[7,0],[1,9],[3,0],[2,7],[5,3],[6,0],[3,4],[6,2],[5,2]].
    The expected output is
    [[3,0],[6,0],[7,0],[5,2],[3,4],[5,3],[6,2],[2,7],[9,0],[1,9]]
    and that is my program's output in my computer.
    but my program's output by leetcode submission is
    [[9,0],[2,7],[3,0],[3,4],[5,3],[5,2],[6,2],[6,0],[7,0],[1,9]]

    Here is my code by c#, does someone can tell me what did I do wrong?

     class leetcode_406
        {
            public int[][] make2DArray(int[,] a)
            {
                int h = a.GetLength(0), w = a.GetLength(1);
                int[][] arr = new int[h][];
    
                for (int i=0; i<h; i++)
                {
                    int[] row = new int[w];
                    for (int j=0; j<w; j++)
                    {
                        row[j] = a[i, j];
                    }
                    arr[i] = row;
                }
                return arr;
            }
    
            private class heightComparer : IComparer<int[]>
            {
                int IComparer<int[]>.Compare(int[] x, int[] y)
                {
                    return (x[0]>=y[0])?-1:1;
                }
    
            }
    
            private class numberComparer : IComparer<int[]>
            {
                int IComparer<int[]>.Compare(int[] x, int[] y)
                {
                    if (x[0] == y[0])
                        return (x[1] >= y[1]) ? 1 : -1;
                    else
                        return 0;
                }
            }
    
            public int[,] ReconstructQueue(int[,] people)
            {
                int[][] arr = make2DArray(people);
                int[,] ans = new int[people.Length/2,2];
                Stack<int> indexStack = new Stack<int>();
                Stack<int> popedIndex = new Stack<int>();
                int i = 0;
    
                Array.Sort(arr, new heightComparer());
                Array.Sort(arr, new numberComparer());
    
                while(indexStack.Count != arr.Length)
                {
                    if ( indexStack.Count != 0 && 
                         arr[i][0] <= arr[indexStack.Peek()][0] &&
                         arr[i][1] < indexStack.Count)
                    {
                        popedIndex.Push(indexStack.Pop());
                    }
                    else
                    {
                        indexStack.Push(i);
                        i++;
                        while (popedIndex.Count != 0)
                            indexStack.Push( popedIndex.Pop() );
                    }
                }
    
                i = arr.Length-1;
                foreach( int index in indexStack)
                {
                    ans[i, 0] = arr[index][0];
                    ans[i, 1] = arr[index][1];
                    i--;
                }
    
                return ans;
            }
        }
    

Log in to reply
 

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