4ms(fastest) 6 line C++ solution


  • 1
    L

    Just sort the array in ascending order.
    Now for each index check if the value i.e arr[i] is more than (size-i). If true then return (size-i).
    At any index idx, if the number of elements on right hand sides are greater or equals to the present value then it should be the h-index of the array.

    int hIndex(vector<int>& citations) {

        int size = citations.size(),k,i, max_h=0;
        sort(citations.begin(), citations.end());
    
        for(int i=0;i<size;i++)
            if(citations[i]>=size-i)
                return size-i;
                
        return 0;
    }

Log in to reply
 

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