274. 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.

s思路:
1. 涨指数了,h-index这么计算的。首先想想简单粗暴的计算:排序。例如,[3, 0, 6, 1, 5]递减排序得到[6,5,3,1,0],然后遍历,如果遍历到3,3等于坐标+1,所以继续遍历,1就小于坐标+1,所以返回此时的坐标值。
2. 这样的复杂度就是o(nlgn)。能做到o(n)吗?
3. 看了提示。有o(n)。考虑到h值的边界是0-citations.size();也就是说,h值的范围是confined within size;也就是说,我们可以先扫描一遍,凡是值大于citations.size()的都可以用citations.size()替换,而不影响计算结果;也就是说,现在所有的值也是confined的,妥妥用counting sort就够了,因此复杂度就降低到o(n)。太TM妙了!
4. 找到输出的边界,一切就清楚了!

//方法1:
class Solution {
public:
    int hIndex(vector<int>& citations) {
        //
        sort(citations.begin(),citations.end(),[](int&a,int&b){
             return a>b;   
        });

        for(int i=0;i<citations.size();i++){
            if(citations[i]<i+1) return i;    
        }
        return citations.size();
    }
};


//方法2:先对数据做“削顶处理”,先对超出范围的数削顶,然后就是counting sort,美!
class Solution {
public:
    int hIndex(vector<int>& citations) {
        //

        int n=citations.size();
        vector<int> count(n+1,0);
        for(int i=0;i<citations.size();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 n;
    }
};
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 、可私信6博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 、可私信6博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值