H-Index II
Given an array of citations sorted in ascending order (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.
According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each."
Example:
Input:citations = [0,1,3,5,6]Output: 3 Explanation:[0,1,3,5,6]means the researcher has5papers in total and each of
them had received 0, 1, 3, 5, 6citations respectively. Since the researcher has3papers with at least3citations each
and the remaining two with no more than3citations each,
her h-index is3.
Note:
If there are several possible values for h, the maximum one is taken as the h-index.
class Solution {
public int hIndex(int[] citations) {
int length = citations.length;
int low = 0, high = length - 1, mid;
while (low <= high) {
mid = (low + high) / 2;
if (length - mid > citations[mid])
low = mid + 1;
else
high = mid - 1;
}
return length - low;
}
}
Formally, if f is the function that corresponds to the number of citations for each publication, we compute the h-index as follows: First we order the values of f from the largest to the lowest value. Then, we look for the last position in which f is greater than or equal to the position (we call h this position). For example, if we have a researcher with 5 publications A, B, C, D, and E with 10, 8, 5, 4, and 3 citations, respectively, the h-index is equal to 4 because the 4th publication has 4 citations and the 5th has only 3. In contrast, if the same publications have 25, 8, 5, 3, and 3 citations, then the index is 3 because the fourth paper has only 3 citations.
- f(A)=10, f(B)=8, f(C)=5, f(D)=4, f(E)=3 → h-index=4
- f(A)=25, f(B)=8, f(C)=5, f(D)=3, f(E)=3 → h-index=3
Click here for May month challenges with solution and explanation
Click here for April month challenges with solution and explanation
Comments
Post a Comment