274. H-Index (1)
H值等於表示citation是H以上的paper數
Description
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."
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, her h-index is 3.
Note: If there are several possible values for h, the maximum one is taken as the h-index.
Solution
h值是paper 數,且滿足至少有h篇paper的citation數大於h
class Solution {
public int hIndex(int[] citations) {
int n = citations.length;
//Collect the count of each citation number into the buckets
// and the index is the citation number, the value is the count
// for ex: bucket[0] = 1, means there are one paper which citation count is 0
// for ex: bucket[2] = 3, means there are 3 papers which citation count is 2
int[] buckets = new int[n+1];
for(int i = 0; i < n; i++){
if(citations[i] > n)
{
buckets[n]++;
}
else{
buckets[citations[i]]++;
}
}
//iterate the buckets from back to front
//and calucalate the paperCount for each iteration
int count = 0;
//count表示citation是i以上的paper數
for(int i = n; i >= 0; i--) {
count += buckets[i];
if(count >= i) {
return i;
}
}
return 0;
}
}
Last updated
Was this helpful?