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 has5papers in total and each of them had received3, 0, 6, 1, 5citations respectively. Since the researcher has3papers with at least3citations each and the remaining two with no more than3citations each, her h-index is3.
Note: If there are several possible values for h, the maximum one is taken as the h-index.
H指數(H index)是一個混合量化指標,可用於評估研究人員的學術產出數量與學術產出水平
可以按照如下方法確定某人的H指數:
將其發表的所有SCI論文按被引次數從高到低排序;
從前往后查找排序后的列表,直到某篇論文的序號大於該論文被引次數。所得序號減一即為H指數。
解法1: 先將數組排序,T:O(nlogn), S:O(1)。然后對於每個引用次數,比較大於該引用次數的文章,取引用次數和文章數的最小值,即 Math.min(citations.length-i, citations[i]),並更新 level,取最大值。排好序之后可以用二分查找進行遍歷,這樣速度會更快,可見:275. H-Index II H指數 II
解法2: Counting sort,T:O(n), S:O(n)。使用一個大小為 n+1 的數組count統計引用數,對於count[i]表示的是引用數為 i 的文章數量。從后往前遍歷數組,當滿足 count[i] >= i 時,i 就是 h 因子,返回即可,否則返回0。
為什么要從后面開始遍歷? 為什么 count[i] >= i 時就返回?
一方面引用數引用數大於 i-1 的數量是i-1及之后的累加,必須從后往前遍歷。另一方面,h 因子要求盡可能取最大值,而 h 因子最可能出現最大值的地方在后面,往前值只會越來越小,能盡快返回就盡快返回,所以一遇到 count[i] >= i 就返回。參考:Code_Granker
Java:
public class Solution {
public int hIndex(int[] citations) {
Arrays.sort(citations);
int level = 0;
for(int i = 0; i < citations.length; i++)
level = Math.max(level,Math.min(citations.length - i,citations[i]));
return level;
}
}
Java:
public class Solution {
public int hIndex(int[] citations) {
int n = citations.length;
int[] count = new int[n + 1];
for(int c : citations)
if(c >= n) count[n]++; //當引用數大於等於 n 時,都計入 count[n]中
else count[c]++;
for(int i = n; i > 0; i--) { //從后面開始遍歷
if(count[i] >= i) return i;
count[i-1] += count[i]; //引用數大於 i-1 的數量是i-1及之后的累加
}
return 0;
}
}
Python: Counting sort.
class Solution(object):
def hIndex(self, citations):
"""
:type citations: List[int]
:rtype: int
"""
n = len(citations);
count = [0] * (n + 1)
for x in citations:
# Put all x >= n in the same bucket.
if x >= n:
count[n] += 1
else:
count[x] += 1
h = 0
for i in reversed(xrange(0, n + 1)):
h += count[i]
if h >= i:
return i
return h
Python: T: O(nlogn) O: O(1)
class Solution2(object):
def hIndex(self, citations):
"""
:type citations: List[int]
:rtype: int
"""
citations.sort(reverse=True)
h = 0
for x in citations:
if x >= h + 1:
h += 1
else:
break
return h
Python: T: O(nlogn) O: O(n)
class Solution3(object):
def hIndex(self, citations):
"""
:type citations: List[int]
:rtype: int
"""
return sum(x >= i + 1 for i, x in enumerate(sorted(citations, reverse=True)))
Python:
class Solution(object):
def hIndex(self, citations):
"""
:type citations: List[int]
:rtype: int
"""
if not citations: return 0
return max([min(i + 1, c) for i, c in enumerate(sorted(citations, reverse=True))])
C++:
class Solution {
public:
int hIndex(vector<int>& citations) {
sort(citations.begin(), citations.end(), greater<int>());
for (int i = 0; i < citations.size(); ++i) {
if (i >= citations[i]) return i;
}
return citations.size();
}
};
類似題目:
[LeetCode] 275. H-Index II H指數 II
All LeetCode Questions List 題目匯總
