H-Index

Given an array of integers citations where citations[i] is the number of citations a researcher received for their i-th paper. Please compute the researcher's h-index.

According to the definition of h-index on Wikipedia: A scientist has an 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.

If there are several possible values for h, the maximum one is taken as the h-index.

DO NOT use any built-in library functions for sorting, e.g., std::sort.

Example

Input: 3 0 6 1 5
Output: 3

Explanation: [3,0,6,1,5] 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/her h-index is 3.

Reference Code

#include <iostream>
#include <vector>
#include <sstream>
using namespace std;

int divide(vector<int>& a, int low, int high) //划分函数
{
    int  k = a[low];
    do
    {
        while(low < high && a[high] >= k)
        {
            --high;
        }
        if(low < high)
        {
            a[low] = a[high];
            ++low;
        }
        while(low < high && a[low] <= k)
        {
            ++low;
        }
        if(low < high)
        {
            a[high] = a[low];
            --high;
        }
    }while(low != high);
    a[low] = k;
    return low;
}

void quickSort(vector<int>& a, int low, int high)
{
    int mid;
    if(low >= high)
    {
        return;
    }
    mid = divide(a, low, high);
    quickSort(a, low, mid-1); //排序左一半
    quickSort(a, mid+1, high); //排序右一半
}

void quickSort(vector<int>& a, int size) //包裹函数
{
    quickSort(a, 0, size-1);
}

class Solution
{
public:
    int hIndex(vector<int>& citations)
    {
    	// Compute h-index
        int sz=citations.size();
        quickSort(citations,sz);

        int h=0;
        int tmp=0;
        for(int i=0;i<sz;i++)
        {
            tmp=min(citations[i],sz-i);
            if(tmp>h)
            {
                h=tmp;
            }
        }
        return h;
    }
};

int main()
{
    // Process input line
    string str;
    getline(cin, str);
    stringstream input(str);

    vector<int> citations;
    double a;

    while(input >> a)
    {
        citations.push_back(a);
    }
    // Return the result

    Solution s;

    int h;

    h=s.hIndex(citations);

    cout<<h;

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值