274. H-Index

来源:互联网 发布:配电网数据采集与监控 编辑:程序博客网 时间:2024/04/30 08:19

Problem

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.

Solution

按照定义,排序之后暴力法,暴力法

class Solution {public:    int hIndex(vector<int>& citations) {    if(citations.empty())        return 0;    sort(citations.begin(),citations.end());    int size = citations.size();    int maxEle = *max_element(citations.begin(),citations.end());    int hIndex_ = 0;    for(int i = 1;i<=maxEle;++i)    {        int count = 0;        for(int j = size-1;j>=0;--j)        {            if(citations[j] >=i)                ++count;            if(count >= i && i>hIndex_)                hIndex_ = i;        }    }    return hIndex_;    }};

讨论区的方法

带有解释的讨论
首先我们可以知道,一个人的h-index最大只可能是paper的篇数L。
我们使用一个数组,来记录引用数为i的论文的篇数,对于引用数大于paper篇数的情况,统统算在引用数为L的情况中。
然后从L开始,从大到小累加篇数,找到h-index。

class Solution {public:    int hIndex(vector<int>& citations) {    if(citations.empty())        return 0;    int size = citations.size();    vector<int> count(size+1,0);    for(int i = 0;i<citations.size();++i)    {        if(citations[i] > size)        {            count[size]++;        }        else        {            count[citations[i]]++;        }    }    int total = 0;    for(int i = size;i>=0;i--)    {        total+=count[i];        if(total >=i)            return i;    }    return 0;    }};
0 0
原创粉丝点击