Straightforward standard very clean binary search.


  • 0
    M

    The idea is simple, if you want to have x as h-index, your citation[N-x] must have at least x citations. This is simplified like finding the largest number that satisfy citation[N-x]>=x. So like Find the First Bad Version, code is simple BS:

    public int hIndex(int[] c) {
    		int N = c.length;
    		int lo = 0, hi = N;
    		while (lo < hi) {
                // only different, +1 to avoid infinity loop when mid == lo 
    			int mid = (hi + lo + 1) / 2;
    			if (c[N - mid] >= mid) {
    				lo = mid;
    			} else {
    				hi = mid - 1;
    			}
    		}
    		return lo;
    }

Log in to reply
 

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