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.
According to thedefinition of h-index on Wikipedia: "A scientist has indexhifhof his/herNpapers haveat leasthcitations each, and the otherN − hpapers haveno more thanhcitations each."
For example, givencitations = [3, 0, 6, 1, 5], which means the researcher has5papers in total and each of them had received3, 0, 6, 1, 5citations respectively. Since the researcher has3papers withat least3citations each and the remaining two withno more than3citations each, his h-index is3.
Note: If there are several possible values forh, the maximum one is taken as the h-index.
class Solution {
public int hIndex(int[] citations) {
int n = citations.length;
int[] freq = new int[n + 1];
for (int c : citations) {
if (c >= n) {
freq[n]++;
} else {
freq[c]++;
}
}
int count = 0;
for (int i = n; i >= 0; i--) {
count += freq[i];
if (count >= i) {
return i;
}
}
return 0;
}
}