leetcode 274. H-Index

第一种比较直接的想法,但往往不是最简单的,先排序然后一个一个对应,但往往这种情况下不会是最好的办法

static bool comp(int a, int b)
{
    return !(a<b);
}
int hIndex(vector<int>& citations) 
{
    citations.push_back(INT_MAX);
    sort(citations.begin(), citations.end(), Solution::comp);
    int temp=0;
    for (int i = 1; i < citations.size(); i++)
    {
        if (citations[i] >= i)
            temp = i;
    }
    return temp;
}

第二种办法 利用一个性质一共n个数,他的H-index一定不会超过n;然后从后往前处理
int hIndex(vector& citations) {
int n = citations.size();
if (!n)
return 0;
vector record(n+1, 0); //以前想的是申请最大的数对应大小的空间,但这步是多余的
for (int i = 0; i < n; i++)
if (citations[i] >= n)
record[n]++;
else
record[citations[i]]++;
int count = 0;
for (int i = n; i >= 0; i–)
{
count += record[i]; //这一步是紧跟定义,有点绕,但是想清楚了就行。。。
if (count >= i)
return i;
}
}
There are always better solutions for us beginners.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值