美文网首页
274. H-Index

274. H-Index

作者: FlynnLWang | 来源:发表于2016-12-28 08:22 被阅读0次

Question

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.
Hint:

  1. An easy approach is to sort the array first.
  2. What are the possible values of h-index?
  3. A faster approach is to use extra space.

Code

public class Solution {
    public int hIndex(int[] citations) {
        // if (citations == null || citations.length == 0) return 0;
        
        // Arrays.sort(citations);
        // for (int i = 0 ; i < citations.length; i++) {
        //     if (citations[i] >= citations.length - i) return citations.length - i;
        // }
        // return 0;
        if (citations == null || citations.length == 0) return 0;
        
        int n = citations.length;
        int[] count = new int[n + 1];
        for (int i = 0; i < n; i++) {
            if (citations[i] >= n) count[n]++;
            else count[citations[i]]++;
        }
        
        int sum = 0;
        for (int i = n; i > 0; i--) {
            sum += count[i];
            if (sum >= i) return i;
        }
        return 0;
    }
}

Solution

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

也可以不对数组排序,额外使用一个大小为N+1的数组stats。stats[i]表示有多少文章被引用了i次,这里如果一篇文章引用大于N次,就将其当为N次,因为H指数不会超过文章的总数。为了构建这个数组,需要先将整个文献引用数组遍历一遍,对相应的格子加一。统计完后,从N向1开始遍历这个统计数组。如果遍历到某一个引用次数时,大于或等于该引用次数的文章数量,大于引用次数本身时,可以认为这是H指数。之所以不用再向下找,因为要取最大的H指数。那如何求大于或等于某个引用次数的文章数量呢?可以用一个变量,从高引用次的文章数累加下来。因为如果有x篇文章的引用大于等于3次,那引用大于等于2次的文章数量一定是x加上引用次数等于2次的文章数量。

相关文章

  • 2019-02-05

    LeetCode 274. H-Index Description Given an array of citat...

  • ARTS 第22周

    ARTS 第22周分享 [TOC] Algorithm 274. H-Index [medium] [题目描述] ...

  • 274. H-Index

    笨方法,写完就睡着了 如果是排好序就很容易了,代码如下:

  • 274. H-Index

    Question Given an array of citations (each citation is a ...

  • 274. H-Index

    Given an array of citations (each citation is a non-negat...

  • 274. H-Index

    问题 Given an array of citations (each citation is a non-ne...

  • 274. H-Index

  • 274. H-Index

    问题描述 Given an array of citations (each citation is a non-...

  • 274. H-Index, 275. H-Index II

    274 就先sort一下,再遍历一遍从高到低排序,然后从左向右扫。如果一个数的值大于等于他的index + 1,则...

  • Leetcode解题报告——274. H-Index

    题目要求:Given an array of citations (each citation is a non-...

网友评论

      本文标题:274. H-Index

      本文链接:https://www.haomeiwen.com/subject/rqfyvttx.html