H指数

发布时间 2023-12-02 15:32:32作者: kte66

H指数

难度 : 简单 | 中等 | 困难 ------------------- 用时:20分钟(第一次) ------------------- 作题日期:2023-12-02
ps: 本人理解有限,以下是自我理解,官方和大佬有更完整和详细的解析!!!

题目描述

  • 题目描述
    给你一个整数数组 citations ,其中 citations[i] 表示研究者的第 i 篇论文被引用的次数。计算并返回该研究者的 h 指数。
    根据维基百科上 h 指数的定义:h 代表“高引用次数” ,一名科研人员的 h 指数 是指他(她)至少
    发表了 h 篇论文,并且每篇论文 至少 被引用 h 次。如果 h 有多种可能的值,h 指数 是其中最大的那个。
    
  • 案例演示
    示例 1:
    
    输入:citations = [3,0,6,1,5]
    输出:3 
    解释:给定数组表示研究者总共有 5 篇论文,每篇论文相应的被引用了 3, 0, 6, 1, 5 次。
        由于研究者有 3 篇论文每篇 至少 被引用了 3 次,其余两篇论文每篇被引用 不多于 3 次,所以她的 h 指数是 3。
    示例 2:
    
    输入:citations = [1,3,1]
    输出:1
    
  • 提示
    n == citations.length
    1 <= n <= 5000
    0 <= citations[i] <= 1000
    

代码及解析

两次循环查找(时间效率较低)

我们可以发现h 指数不可能大于总的论文发表数,所以对于引用次数超过论文发表数的情况,我们可以将其按照总的论文发表数来计算即可。

class Solution {
    public int hIndex(int[] citations) {
        int[] count  = new int[citations.length+1];
        int max = 0;
        for(int i = 0; i<citations.length;i++){
            for (int j = 1; j < count.length; j++) {
                if(citations[i]>=j){
                    count[j]++;
                }
            }
        }
        for (int i = 1; i < count.length; i++) {
            if(count[i]>=i){
                max = i;
            }
        }
        return max;
    }
}

第二次:排序

根据h的定义,至少有h篇使用了至少被引用h次,我们先用排序,然后倒序遍历,当citations[i] > ans,说明该文献被引用次数是大于目前最大的引用次数,所以总篇数和被引用次数共同增加当citations[i] == ans 即可返回答案即可。

class Solution {
    public int hIndex(int[] citations) {
        int ans = 0;
        Arrays.sort(citations);
        int i = citations.length - 1; 
        while (i >= 0 && citations[i] > ans) {
            ans++; 
            i--;
        }
        return ans;
    }
}