My 3 lines brute force and 7 lines binary search


  • 0
    K
    int hIndex(vector<int>& citations) {
            int i = 0, len = citations.size();
            for (; i < len  && citations[len-i-1] >= i+1; ++i);
            return i;
        }
    
    int hIndex(vector<int>& citations) {
            int lo = 0, hi = citations.size(), len = citations.size();
            while (lo < hi) {
                int mid = lo + (hi-lo) / 2;
                if (citations[len - mid - 1] >= mid + 1) lo = mid + 1;
                else hi = mid;
            }
            return lo;
        }
    

Log in to reply
 

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