Binary Search with small hack. 10 ms solution.


  • 0
    P
    public int hIndex(int[] citations) {
            int len = citations.length;
            if(len == 0)
                return 0;
            int end = len - 1;
            int mid = end/2;
            int start = 0;
            while(start < end) { // Usual stuff.
                if(citations[mid] >= len - mid) // H-Index check.
                    end = mid;
                else
                    start = mid + 1;
                mid = (start + end)/2; // This will ensure the correct mid value.
    
            }
            if(citations[mid] >= len - mid) // Final check.
                return len - mid;
            else
                return 0;
        }
    

Log in to reply
 

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