Share my C++ solution - O(1) space and O(nlogn) time


  • 0
    S
    int hIndex(vector<int> &citations) {
        int result = 0;
        sort(citations.begin(), citations.end());
    
        for(int i=citations.size()-1; i >= 0; --i) {
            if (citations.size() - i <= citations[i] ) {
                result = min(static_cast<int>(citations.size()-i), citations[i]);
            }
        }
    
        return result;
    }

Log in to reply
 

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