Easy O(logn) C++ 8ms solution


  • 3
    J
    int hIndex(vector<int>& citations) {
        int size = citations.size();
        if (size == 0) {
            return 0;
        }
        int l = 0, r = size - 1; 
        while (l < r) {
            int m = (l + r) / 2;
            if (citations[m] < size - m) {
                l = m + 1;
            } else {
                r = m;
            }
        }
        if (!citations[r]) {
            return 0;
        } else {
            return size - r;
        }
    }

Log in to reply
 

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