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 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."
For example, given citations = [3, 0, 6, 1, 5]
, which means the researcher has 5
papers in total and each of them had received 3, 0, 6, 1, 5
citations respectively. Since the researcher has 3
papers with at least 3
citations each and the remaining two with no more than 3
citations each, his h-index is 3
.
Note: If there are several possible values for h
, the maximum one is taken as the h-index.
/**
* 题目的意思是给定数组中每个元素表示其对应文章的引用次数,求一个引用次数h,使得所有的引用次数中有h次引用次数是大于等于h,
* 其他的N-h个引用次数小于h。
* 参见http://blog.csdn.net/xudli/article/details/48226129。
* 做法是为计数排序的变型。
* 首先定义一个数组countArray,其下标为i的元素表示引用次数为i的在原数组中出现了次数,countArray[i]表示引用次数大于等于n的有几次。
* 然后从后往前遍历数组,更新countArray[i] = countArray[i] + countArray[i+1]表示引用次数大于等于i的在原数组中出现的次数,
* 直到遇到countArray[i]>=i,即表示找到了h = i表示原数组中有i个元素的值大于等于i,有n-i个元素值不大于i
* @date 20150512
*/
public int hIndex(int[] citations) {
int n = citations.length;
int[] countArray = new int[n+1];
/*下标为i的元素表示引用次数为i的在原数组中出现了次数,countArray[i]表示引用次数大于等于n的有几次*/
for(int i=0; i<n; i++) {
if(citations[i] >= n) countArray[n]++;
else countArray[citations[i]]++;
}
/*然后从后往前遍历数组,更新countArray[i] = countArray[i] + countArray[i+1]表示引用次数大于等于i的在原数组中出现的次数,
* 直到遇到countArray[i]>=i,即表示找到了h = i表示原数组中有i个元素的值大于等于i,有n-i个元素值不大于i*/
if(countArray[n]>=n) return n;
for(int i=n-1; i>=0; i--) {
countArray[i] = countArray[i] + countArray[i+1];
if(countArray[i]>=i) return i;
}
return 0;
}
还有另外一种做法是:
http://blog.csdn.net/canglingye/article/details/48207011