# [LeetCode] 274. H-Index H指数

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.

H指数（H index）是一个混合量化指标，可用于评估研究人员的学术产出数量与学术产出水平

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 题目汇总

posted @ 2018-03-27 02:24  轻风舞动  阅读(1568)  评论(0编辑  收藏