归档

274. H指数

274. H指数

题目

描述:

给你一个整数数组 citations ,其中 citations[i] 表示研究者的第 i 篇论文被引用的次数。计算并返回该研究者的 h 指数。

根据维基百科上 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 指数为 h,意味着至少有 h 篇论文的引用次数不小于 h,并且找不到更大的可行值。论文一共只有 n 篇,所以答案一定在 0 到 n 之间。

准备长度为 n + 1 的计数数组 counter。对于 i < n,counter[i] 表示恰好被引用 i 次的论文数量;counter[n] 表示被引用至少 n 次的论文数量。把大于 n 的引用次数放进最后一个桶不会影响答案,因为我们不会检查大于 n 的阈值。

然后从阈值 n 向下扫描,用 total 累加引用次数至少达到当前阈值的论文数量。如果 total >= i,说明至少有 i 篇论文被引用了至少 i 次,i 就是可行答案。由于更大的阈值已经检查过并且失败,第一个可行值就是最大值。

例如 [3,0,6,1,5] 共 5 篇论文,计数数组为 [1,1,0,1,0,2]。检查阈值 5 时只有 2 篇达标;检查 4 时仍为 2 篇;检查 3 时有 3 篇,首次满足条件,因此返回 3。全部引用次数为 0 时,会一直检查到阈值 0,返回 0。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public int hIndex(int[] citations) {
    int n = citations.length;
    int[] counter = new int[n + 1];

    for (int c : citations) {
        if (c >= n) {
            counter[n]++;
        } else {
            counter[c]++;
        }
    }

    int total = 0;
    for (int i = n; i >= 0; i--) {
        total += counter[i];
        if (total >= i) {
            return i;
        }
    }

    return 0;
}

时间复杂度是 $O(n)$,因为只做了两次线性遍历;空间复杂度是 $O(n)$,因为额外开了一个长度为 n + 1 的计数数组。

完整示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.util.Arrays;

public class Main {

    public static int hIndex(int[] citations) {
        int n = citations.length;
        int[] counter = new int[n + 1];

        for (int c : citations) {
            if (c >= n) {
                counter[n]++;
            } else {
                counter[c]++;
            }
        }

        int total = 0;
        for (int i = n; i >= 0; i--) {
            total += counter[i];
            if (total >= i) {
                return i;
            }
        }

        return 0;
    }

    public static int hIndexSorted(int[] citations) {
        Arrays.sort(citations);
        int n = citations.length;

        for (int i = 0; i < n; i++) {
            int h = n - i;
            if (citations[i] >= h) {
                return h;
            }
        }

        return 0;
    }

    public static void main(String[] args) {
        int[] citations1 = new int[]{3, 0, 6, 1, 5};

        System.out.println("示例 1 输入:");
        System.out.println("citations = " + Arrays.toString(citations1));

        int result1 = hIndex(citations1);

        System.out.println("示例 1 输出: " + result1);

        int[] citations2 = new int[]{1, 3, 1};

        System.out.println("示例 2 输入:");
        System.out.println("citations = " + Arrays.toString(citations2));

        int result2 = hIndex(citations2);

        System.out.println("示例 2 输出: " + result2);
    }
}

运行结果(OpenJDK 21.0.12):

1
2
3
4
5
6
示例 1 输入:
citations = [3, 0, 6, 1, 5]
示例 1 输出: 3
示例 2 输入:
citations = [1, 3, 1]
示例 2 输出: 1
本文由作者按照 CC BY 4.0 进行授权