Leetcode 0274: 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.”

中文描述:

给定一位研究者论文被引用次数的数组(被引用次数是非负整数)。编写一个方法,计算出研究者的 h 指数。
h 指数的定义:h 代表“高引用次数”(high citations),一名科研人员的 h 指数是指他(她)的 (N 篇论文中)总共有 h 篇论文分别被引用了至少 h 次。且其余的 N - h 篇论文每篇被引用次数 不超过 h 次。
例如:某人的 h 指数是 20,这表示他已发表的论文中,每篇被引用了至少 20 次的论文总共有 20 篇。

Example:

Input: citations = [3,0,6,1,5]
Output: 3
Explanation: [3,0,6,1,5] 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, her h-index is 3.

Note:

If there are several possible values for h, the maximum one is taken as the h-index.

Time complexity: O O O( N N N)
Space complexity: O O O( N N N)
计数排序:

  1. 一般做法从大到小排序降序排序后,在排序后的数组[0,i]区间内有i+1篇论文,且每篇文章的引用次数至少是 c i t a t i o n s [ i ] \mathrm{citations}[i] citations[i]. 如果 c i t a t i o n s [ i ] \mathrm{citations}[i] citations[i] > \gt > i i i,那么说明第 0 到 i 篇论文都有至少 i+1次引用。因此我们只要找到最大的i满足 c i t a t i o n s [ i ] \mathrm{citations}[i] citations[i] > \gt > i i i,那么 h 指数即为 i+1(注意排序后数组index从0开始。此做法 Time complexity: O O O( N l o g N NlogN NlogN)
  2. 注意到 在计算h index的过程中,论文的引用次数可能会非常多,这个数值很可能会超过论文的总数 n。但是如果一篇文章的引用次数超过论文的总数 n,那么将它的引用次数降低为 n 也不会改变 h 指数的值。所以建一个count 数组统计 各个引用次数(0 ~ n )出现的频率(即当前的引用次数 的论文个数) 当引用次数超过论文的总数 n时也计为n。因为是求最大值,初始化 h指数为 t = n , 当前的引用次数 的论文个数为 cnt = count[n], 如果 cnt >= t, 则表明被引用了至少 n 次的论文至少有 n 篇。则t = n 为h指数。如果 cnt < t, 怎h指数为 t 的值减1:t–,同时 cnt += count[t]; 表示要加上此时引用次数 的论文个数。循环直到cnt >= t。
class Solution {
    
    
    public int hIndex(int[] citations) {
    
    
        int n = citations.length;
        int[] count = new int[n + 1];
        for (int c: citations){
    
    
            count[Math.min(n, c)]++;
        }
        int t = n;
        int cnt = count[n];
        while(cnt < t){
    
    
            t--;
            cnt += count[t];
        }
        return t;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43946031/article/details/114110056