h引用(数组中有h个数大于等于h,其余小于)H-Index

问题:

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。

① 排序法。先将数组排序,我们就可以知道对于某个引用数,有多少文献的引用数大于这个数。对于引用数citations[i],大于该引用数文献的数量是citations.length - i,而当前的H-Index则是Math.min(citations[i], citations.length - i),我们将这个当前的H指数和全局最大的H指数来比较,得到最大H指数。

1)

class Solution { //4ms
    public int hIndex(int[] citations) {
        Arrays.sort(citations);
        int h = 0;//全局最大的H指数
        for (int i = 0;i < citations.length;i ++){
            int curH = Math.min(citations[i],citations.length - i);//得到当前的H指数
            if (curH > h){
                h = curH;
            }
        }
        return h;
    }
}

2)

class Solution{//2ms
    public int hIndex(int[] citations) {
        Arrays.sort(citations);
        for (int i = 0; i < citations.length; i ++) {
            if (citations[i] >= citations.length - i) {
                return citations.length - i;
            }
        }
        return 0;
    }
}

② 数组映射法。我们额外使用一个大小为N+1的数组dp。dp[i]表示有多少文章被引用了i次,这里如果一篇文章引用大于N次,我们就将其当为N次,因为H指数不会超过文章的总数。

1)

class Solution { //1ms
    public int hIndex(int[] citations) {
        int len = citations.length;
        int[] dp = new int[len + 1];
        for (int i = 0;i < len;i ++){//统计各个引用次数对应多少篇文章
            int index = citations[i] <= len ? citations[i] : len;
            dp[index] += 1;
        }
        int count = 0;
        for (int i = len;i > 0;i --){
            count+= dp[i];
            if (count >= i){
                return i;
            }
        }
        return 0;
    }
}

2)

class Solution { //1ms
    public int hIndex(int[] citations) {
        int[] dp = new int[citations.length + 1];
        for (int n : citations){
            if (n >= citations.length){
                dp[citations.length] ++;
            }else{
                dp[n] ++;
            }
        }
        int count = 0;
        for (int i = citations.length;i > 0;i --){
            count += dp[i];
            if (count >= i){
                return i;
            }
        }
        return 0;
    }
}

转载于:https://my.oschina.net/liyurong/blog/1591813

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值