Very straightforward c++ solution


  • 0
    Q

    sort the vector, then go through vector and reduce the possible h-index by 1 if element is less than it until the first element which is larger than it occurs.

    int hIndex(vector<int>& citations) {
        
        std::sort(citations.begin(), citations.end());
        
        int result = citations.size();
        
        for(int i = 0; i < citations.size(); i++)
        {
            if (citations[i] < result)
                result--;
            else
                break;
        }
        
        return result;
    }

Log in to reply
 

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