clear java code using binary search, easy to understand


  • 1
    M
    public int hIndex(int[] citations) {
        int n=citations.length;
        int lo=0,hi=n;
        while(lo<hi){
            int mid=(lo+hi+1)/2;//mid represents current h value, not the current position 
            if(citations[n-mid]<mid){
                hi=mid-1;
            }else{
                lo=mid;
            }
        }
        return lo;
    }
    

Log in to reply
 

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