假设我们有一个研究人员的一系列引文(引文是非负整数)。我们必须定义一个函数来计算研究人员的h指数。根据h-index的定义:“如果科学家的N篇论文中的h篇每篇至少被h引用,而其他N - h篇论文每篇不超过h篇引用,则科学家具有h索引。”
因此,如果输入像引文= [3,0,6,1,7],那么输出将为3,因为这表明研究人员有五篇论文,他们分别得到了3、0、6、1、7个引文分别。由于研究人员有3篇论文每篇被至少引用3次,其余两篇论文每篇被引用都不超过3次,因此h指数为3。
为了解决这个问题,我们将遵循以下步骤-
n:=数组的大小,创建一个称为大小为n + 1的存储桶的数组
对于i,范围为0至n – 1
x:= c [i]
如果x> = n,则将bucket [n]加1,否则将bucket [x]加1
cnt:= 0
对于i在n范围内降至0 −
通过存储桶增加[cn]
如果cnt> = i,则返回i
返回– 1
让我们看下面的实现以更好地理解-
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int hIndex(vector<int>& c) {
int n = c.size();
vector <int> bucket(n + 1);
for(int i = 0; i < n; i++){
int x = c[i];
if(x >= n){
bucket[n]++;
} else {
bucket[x]++;
}
}
int cnt = 0;
for(int i = n; i >= 0; i--){
cnt += bucket[i];
if(cnt >= i)return i;
}
return -1;
}
};
main(){
Solution ob;
vector<int> v = {3,0,6,1,7};
cout << (ob.hIndex(v));
}[3,0,6,1,7]
输出结果
3