Concise and simple C++ solution; O(n logn) time, O(1) memory;


  • -2
    A

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

    class Solution {
    public:
    int hIndex(vector<int>& citations) {
    int i;
    sort(citations.begin(), citations.end(), comp);
    for(i = 0 ; i < citations.size(); i++)
    if(citations[i] < i+1)
    break;
    return i; //break when value is greater than index+1
    }
    };


Log in to reply
 

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