Simple 13ms Java solution


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

  • 0
    I

    Why is the condition citations[mid] >= (len - mid) and not citations[mid] == (len - mid)? H index is the biggest number h such that there are h papers with at least h citations, right? so that means for a number to be a valid h index, citations[mid] must equal len - mid, cause len - mid is the # of papers that have at least citations[mid] citations.


  • 0
    K

    because [100,100]


Log in to reply
 

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