O(NlogN) c++ solution 16ms

  • 1
    class Solution {
        int hIndex(vector<int>& citations) {
            if(citations.size()==0)return 0;
            sort(citations.begin(),citations.end(),[](const int &a,const int &b){return a>b;});
            for(int i=citations.size()-2;i>=0;--i){
                if(citations[i]>i && citations[i+1]<=i+1)return i+1;
            if(citations.back()>=citations.size())return citations.size();
            return 0;

  • 0

    Great code! I run it again and it only takes 12ms :-)

  • 0

    Thanks, man.

Log in to reply

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