Calculate H-index

题目:

h-score is a score that publishers get if articles that they publish get citations in other articles. A publisher gets h-score of x if they wrote at least x articles that got x citations each. Given an array with the number of citations per article, write a function to calculate the h-score for the publisher. 


For example. If I have an array of {0,2,3,5,9,7}, h-index for it would be 3 since there are 3 numbers bigger than 3. If we have {0,2,3,5,9,7,6}, h-index would be 4.

思路:

这个题是counting sort的变形。我们也是建一个比原数组长度多一的数组来计数。

对于这个计数数组, count[i] = j means 引用次数为i的论文有j篇。

然后从计数数组的最后一个元素开始往前看。看什么时候累计的数量大于或者等于当前index,返回。

 1 public int cal (int[] arr) {
 2         if (arr == null || arr.length ==0) {
 3             return 0;
 4         }
 5         int[] count = new int[arr.length + 1];
 6         for (int i = 0; i < arr.length; i++) {
 7             count[Math.min(arr[i], arr.length)]++;
 8         }
 9         int sum = 0;
10         for (int i = count.length - 1; i >= 0; i--) {
11             sum += count[i];
12             if (sum >= i) {
13                 return i;
14             }
15         }
16         return 0;
17     }
原文地址:https://www.cnblogs.com/gonuts/p/4652992.html