题目描述:
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.
讲人话:
一名科研人员的h指数是指他至多有h篇论文分别被引用了至少h次。
例如,某人的h指数是20,这表示他已发表的论文中,每篇被引用了至少20次的论文总共有20篇。要确定一个人的h指数非常容易,到SCI网站,查出某个人发表的所有SCI论文,让其按被引次数从高到低排列,往下核对,直到某篇论文的序号大于该论文被引次数,那个序号减去1就是h指数。
这个题比较简单的方法就实现排序,然后按照上面的方法依次找下来即可。如果被引用最少的引用次数都超过了论文数,那么就返回论文数。
例如,有3篇论文,引用次数分别是7,8,9次,那么直接返回3.
代码如下:
public int hIndex(int[] citations) {
Arrays.sort(citations);
int n=citations.length;
for(int i=n-1;i>=0;i--){
if(citations[i]<n-i)
return n-i-1;
}
return 0;
}
这样做的时间复杂度是O(nlog(n))。
这个题其实是counting sort的变种。
代码如下:
public class Solution {
public int hIndex(int[] citations) {
int n = citations.length;
int[] countArray = new int[n+1];
for(int i=0; i<n; i++) {
if(citations[i] >= n) countArray[n]++;
else countArray[citations[i]]++;
}
if(countArray[n]>=n) return n;
for(int i=n-1; i>=0; i--) {
countArray[i] = countArray[i] + countArray[i+1];
if(countArray[i]>=i) return i;
}
return 0;
}
}
在这里复习一下计数排序,代码如下:
private static void sort(int a[], int b[], int k){
//初始化计数数组
int c[] = new int[k];
for(int i = 0; i<k; i++)
c[i] = 0;
//计算数组中重复的次数
for(int i=0; i<a.length; i++)
{
c[a[i]] = c[a[i]]+1;
}
for(int i = 1; i<k; i++)
{
c[i] = c[i]+c[i-1];
}
//将a数组中的元素按照顺序复制到b中
for(int i = a.length-1; i>=0; i--)
{
b[c[a[i]]-1] = a[i];
c[a[i]] = c[a[i]]-1;
}
}