Ac solution code


  • 0

    The main idea is based on Binary Search: find the first index satisfying citations[index] >= length(citations) - index.

    public int hIndex(int[] citations) {
        int len = citations.length, start = 0, end = len - 1;
        while(start <= end) {
            int mid = (end + start) / 2;
            if(citations[mid] == len - mid) 
            	return len - mid;
            else if (citations[mid] > len - mid)
            	end = mid - 1;
            else  
            	start = mid + 1;  
        }             
    	return len - start;
    }

Log in to reply
 

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