Problem: Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index. The h-index is defined as the maximum value of h such that the given author/journal has published h papers that have each been cited at least h times.
Example:
Input:citations = [10,9,8,7,3]
Output: 4 Explanation:[10,9,8,7,4
received 10]
means the researcher has5
papers in total and each of them had, 9, 8, 7, 4
citations respectively. Since the researcher has 4 papers with at least 4 citations each and the remaining one has only3
citations i.e. less than 4, his h-index is 4.
Approach: This is a simple problem to solve. Just use sorting.
Implementation in C#:
public static int HIndex(int[] citations)
{
if (citations?.Length == 0)
{
return 0;
}
Array.Sort(citations);
Array.Reverse(citations);
int hIndex = 0;
for (int i = 0; i < citations.Length; ++i)
{
if (i + 1 > citations[i])
{
break;
}
hIndex = Math.Max(hIndex, Math.Min(i + 1, citations[i]));
}
return hIndex;
}
Complexity: O(nlogn)
No comments:
Post a Comment