My binary search solution


  • 1
    J
    public int hIndex(int[] citations) {
        if(citations.length==0||citations[citations.length-1]<=0) return 0;
        int l=0,r=citations.length-1;
        while(l<r){
            int mid=(r+l)/2;
            if(citations[mid]>citations.length-mid) r=mid;
            else if(citations[mid]<citations.length-mid) l=mid+1;
            else return citations.length-mid;
        }
        return citations.length-l;
    }

Log in to reply
 

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