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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值