天天看点

leetcode笔记: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.”

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.

二. 题目分析

首先需要了解一下题目的大意:

给定一个数组,记载了某研究人员的文章引用次数(每篇文章的引用次数都是非负整数),编写函数计算该研究人员的h指数。

根据维基百科上对h指数的定义:“一名科学家的h指数是指在其发表的

N

篇论文中,有

h

篇论文分别被引用了至少

h

次,其余

N-h

篇的引用次数均不超过

h

次”。

例如,给定一个数组

citations = [3, 0, 6, 1, 5]

,这意味着该研究人员总共有

5

篇论文,每篇分别获得了

3, 0, 6, 1, 5

次引用。由于研究人员有

3

篇论文分别至少获得了

3

次引用,其余两篇的引用次数均不超过

3

次,因而其h指数是

3

注意:如果存在多个可能的

h

值,取最大值作为

h

指数。

通过下图,可以更直观了解

h

值的定义,对应图中,即是球左下角正方形的最大值:

leetcode笔记:H-Index

以下解释中,假设给定数组的大小为

N

,即共有

N

篇文章。

常规的做法有两种,也是题目tips中提到的,首先想到的是将数组进行排序,然后从后往前遍历,找出这个h值,该方法的复杂度是:

O(n*logn)

在面试中,若允许使用辅助内存,可以使用第二种方法,即开辟一个新数组

record

,用于记录

0~N

次引用次数的各有几篇文章(引用次数大于

N

的按照

N

次计算)遍历数组,统计过后,遍历一次统计数组

record

,即可算出

h

值的最大值。时间复杂度为

O(n)

三. 示例代码

// 排序+遍历
class Solution {
public:
    int hIndex(vector<int>& citations) {
        sort(citations.begin(), citations.end(), [](const int &a, const int &b){return a > b; });
        int i = ;
        for (; i < citations.size(); ++i)
            if (citations[i] <= i)
                break;
        return i;
    }
};
           
// 第二种的方法
class Solution {
public:
    int hIndex(vector<int>& citations) {
        int citationSize = citations.size();
        if (citationSize < ) return ;
        vector<int> record(citationSize + , );
        for (int i = ; i < citationSize; ++i)
        {
            if (citations[i] <= citationSize)
                ++record[citations[i]];
            else
                ++record[citationSize];
        }

        for (int j = citationSize, paperNum = ; j >= ; --j)
        {
            paperNum += record[j];
            if (paperNum >= j) return j;
        }
        return ;
    }
};
           

四. 小结

使用何种方法,需要根据实际条件而定。