Simple C++ solution (8ms)


  • 0
    H
    class Solution {
    public:
        int hIndex(vector<int>& citations) {
            int n = citations.size();
            if (n == 0) return 0;
            int i = 0, j = min(n, citations.back())+1;
            while (i < j) {
                int mid = i + (j - i) / 2;
                if (citations[n - mid] == mid || i == mid) return mid;
                else if (citations[n - mid] > mid) i = mid; // Increase h: h can be mid.
                else j = mid;                               // Decrease h.
            }
            return i;
    
        }
    };

Log in to reply
 

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