Runtime Error but it's totally correct when I test it using the test case.


  • 0
    M
    bool myCmp(const int a, const int b){
        if(a >= b)
            return true;
        else
            return false;
    }
    
    class Solution {    
    public:
        int hIndex(vector<int>& citations) {
            sort(citations.begin(), citations.end(), myCmp);
            for(int i = 0; i < citations.size(); i++){
                if(i+1 > citations[i])
                    return i;
            }
            return citations.size();
        }
    };

  • 0

    Re-code your solution as follows:

    class Solution {
    public:
        int hIndex(vector<int>& citations) {
            int size = citations.size();
            if(size == 0) return 0;
            sort(citations.begin(), citations.end(), greater<int>());
            citations.push_back(0); //as a sentinel;
            for(int i = 0; i <= size; ++i)
                if(i > citations[i]-1) return i;
            return 1;
        }
    };
    

    Please format your code in a better way next time, as the <font color="#ff0000">Writing Code</font> points out.


  • 0
    M

    Thanks for your answer.
    Actually I knew the method you offered. The real question which puzzled me is 'why my code failed when I submitted it but the result is correct when I used the failed test case for testing.'
    i.e. what makes the runtime error come out ?

    Thanks for your suggestion for format all the same.


  • 0
    1. you should format your code before replying me actually, your question is quite confusing;
    2. specific details should be provided for further discussion, what do you think? like what cases you mentioned that make your code fail.

    B.T.W. the RE can be caused by the next test not actually the case offered directly by the OJ sometimes.


  • 0
    M

    Sorry, I had format my code now.
    You can try to submit this question using my code above, If you don't mind, and you will find a runtime error and the specific test case. Then if you test it as a Custom Testcase, you will find the output is correct. This is my Question. What makes a runtime error comes out.
    Sorry, English is not my mother tongue. I hope you can understand it . T . T


  • 0
    class Solution {    
    public:
        int hIndex(vector<int>& citations) {
            sort(citations.begin(), citations.end(), greater<int>());
            for(int i = 0; i < citations.size(); i++){
                if(i+1 > citations[i])
                    return i;
            }
            return citations.size();
        }
    };
    

    Or if you just insist to use your own function then you can recode your compare as follows:

    bool myCmp(const int a, const int b){
        return a > b;
    }
    

    You cannot just return a>=b which will disobey the Strict Weak Ordering that sort requires. As for Strict Weak Ordering, what they roughly mean is that a Strict Weak Ordering has to behave the way that "less than" behaves: if a is less than b then b is not less than a, if a is less than b and b is less than c then a is less than c, and so on. But using a>=b in your original compare method might encounter a special case a==b where a>=b is true while meantime b>=a is also true (<font color="#ff0000">a reversible case</font>) and then the result of sort will be undefined, which is why your solution encountered RE.


  • 0
    M

    Amazing!
    Thank you for your patience.


  • 0

    Hahah, it's fine. But maybe you can label it as an answer, which is a good habit for thank you.


  • 0

    I think if this is the answer to your question, you should check it. That's quite necessary for me and for others who might want a peek. Thanks for your cooperation! Dude, it's really badly done.


Log in to reply
 

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