274 H-Index *****more
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 indexhifhof his/herNpapers haveat leasthcitations each, and the otherN − hpapers haveno more thanhcitations each."
Example:
Input:
citations = [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 h-index is 3.
Note: If there are several possible values forh, the maximum one is taken as the h-index.
First O(logn)
class Solution {
public int hIndex(int[] citations) {
if (citations == null || citations.length == 0) {
return 0;
}
Arrays.sort(citations);
//[3,0,6,1,5]
//[0,1,3,5,6]
int len = citations.length;
for (int i = 0; i < citations.length; i++) {
if (citations[i] >= len - i) {
return len - i;
}
}
return 0;
}
}
Java counting sort O(n) solution with detail explanation
This type of problems always throw me off, but it just takes some getting used to. The idea behind it is some bucket sort mechanisms. First, you may ask why bucket sort. Well, the h-index is defined as the number of papers with reference greater than the number. So assumen
is the total number of papers, if we haven+1
buckets, number from 0 to n, then for any paper with reference corresponding to the index of the bucket, we increment the count for that bucket. The only exception is that for any paper with larger number of reference thann
, we put in then
-th bucket.
Then we iterate from the back to the front of the buckets, whenever the total count exceeds the index of the bucket, meaning that we have the index number of papers that have reference greater than or equal to the index. Which will be our h-index result. The reason to scan from the end of the array is that we are looking for the greatest h-index. For example, given array[3,0,6,5,1]
, we have 6 buckets to contain how many papers have the corresponding index. Hope to image and explanation help.
public int hIndex(int[] citations) {
int n = citations.length;
int[] buckets = new int[n+1];
for(int c : citations) {
if(c >= n) {
buckets[n]++;
} else {
buckets[c]++;
}
}
int count = 0;
for(int i = n; i >= 0; i--) {
count += buckets[i];
if(count >= i) {
return i;
}
}
return 0;
}