Run Code inconsistent with Submit Solution


  • 0
    E

    Here is my O(n^2) C++ solution.

        class Solution {
        public:
            static bool cmp( const pair<int,int> & a, const pair<int,int> & b ){
                return a.first > b.first ? true : a.second < b.second;
            }   
            vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) {
                sort( people.begin(), people.end(), cmp );
                vector<pair<int,int> > ret;
                for( int i=0; i<people.size(); i++ ){
                    int j=0, count = 0;
                    for( ; j<ret.size() && count != people[i].second; j++ ){
                        if( ret[j].first >= people[i].first )
                            count++;
                    }   
                    ret.insert( ret.begin()+j, people[i] );
                }   
                return ret;
            }   
        };  
    

    However, when it meets the test case:
    [[13,17],[40,30],[95,6],[11,5],[65,19],[31,51],[79,4],[7,32],[45,1],[61,27],[98,2],[23,43],[4,57],[19,17],[96,0],[25,7],[76,4],[49,28],[72,0],[52,0],[9,40],[90,3],[19,59],[76,9],[68,24],[61,2],[43,42],[98,0],[1,40],[35,37],[86,8],[16,9],[44,7],[84,6],[6,48],[39,8],[30,12],[32,18],[88,10],[0,42],[94,5],[39,33],[92,5],[10,88],[37,25],[86,15],[60,7],[51,27],[39,4],[74,1],[30,45],[67,4],[83,0],[5,93],[81,15],[19,65],[2,61],[28,57],[51,26],[63,35],[43,46],[91,7],[98,1],[4,47],[87,10],[47,34],[53,13],[42,6],[92,2],[73,15],[41,22],[96,2],[53,31],[57,7],[94,1],[40,25],[3,7],[39,54],[79,13],[39,40],[50,10],[84,11],[25,50],[64,19],[83,8],[80,19],[95,1],[35,22],[63,18],[7,61],[31,53],[18,80],[23,65],[70,11],[52,42],[38,56],[20,73],[27,58],[46,2],[94,6]]

    Run Code Result is correct. But Submission Result is "Runtime Error".

    Why?


  • 0
    E

    First, please check if you are using any global or static variables. They are Evil, period. If you must declare one, reset them in the first line of your called method or in the default constructor. Why? Because the judger executes all test cases using the same program instance, global/static variables affect the program state from one test case to another. See this Discuss thread for more details.

    Chances are your code has bugs in it which cause one of the earlier test cases to trigger an undefined behavior. See this Discuss thread for an example of undefined behavior. These bugs could be hard to debug, so good luck. Or just give up on C/C++ entirely and code in a more predictable language, like Java. Just kidding.


  • -1

    @english10010 said in Run Code inconsistent with Submit Solution:

    Are you using C or C++?

    Obviously yes. How did you not see that?


  • 0
    E

    @ManuelP I just copy the solution from Leetcode and forget to delete that sentence. You just reduce my reputation because of this carelessness while ignoring my contribution? You are so rude!


  • 0

    @english10010 said in Run Code inconsistent with Submit Solution:

    You just reduce my reputation because of this carelessness while ignoring my contribution?

    No, I downvoted because of the plagiarism. Which it still is and which is still rude and which still deserves bad reputation. I'll take it back if you edit it and quote properly.


Log in to reply
 

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