Follow up for H-Index: What if the citations
array is sorted in ascending order? Could you optimize your algorithm?
Hint:
- Expected runtime complexity is in O(log n) and the input is sorted.
code:
public class Solution {
public int hIndex(int[] citations) {
int n = citations.length;
int left = 0, right = n-1;
int mid=0;
while(left<=right){
mid = left+(right-left)/2;
if(citations[mid] == n-mid)
return n-mid;
else if(citations[mid]<n-mid){
left = mid+1;
}else
right = mid-1;
}
return n-left;
}
}