LeetCode-274. H 指数-数组+计数排序

Problem: 274. H 指数
每日一题。

思路

理解关系,至少i篇论文引用至少i次,其余len-n篇引用不超过i次。
排序(某条分界线右边满足题意)(可二分查找[1,2,3,4,5]),计数。

Code

数组计数

class Solution {
    public int hIndex(int[] citations) {
        int len = citations.length;
        int[] count = new int[len+1]; // 含len+1计数
        // 记录引用次数
        for(int i:citations){
            count[Math.min(i,len)]++;// 引用次数超过h范围取len
        }
        // 后向前
        for(int i=len,sum=0;i>=0;i--){
            sum += count[i];
            if(sum>=i){
                return i;// 至少i篇论文引用次数至少为i
            }
        }
        return -1;
    }
}

排序+后向前遍历

class Solution {
    public int hIndex(int[] citations) {
        int n = citations.length;
        int h = 0;
        Arrays.sort(citations);
        for (int i = n - 1; i >= 0; i--) {
            // 当前论文的引用次数大于等于 h 时,满足 h 指数的定义
            if (citations[i] >= h + 1) {
                h++;
            } else {
                // 如果当前论文引用次数小于 h,后面的论文引用次数一定更小
                // 因此可以提前结束遍历,返回当前的 h 指数
                break;
            }
        }
        return h;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值