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. 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.
Java Solution 1
public int hIndex(int[] citations) { Arrays.sort(citations); int result = 0; for(int i=citations.length-1; i>=0; i--){ int cnt = citations.length-i; if(citations[i]>=cnt){ result = cnt; }else{ break; } } return result; } |
Java Solution 2
We can also put the citations in a counting sort array, then iterate over the counter array.
public int hIndex(int[] citations) { int len = citations.length; int[] counter = new int[len+1]; for(int c: citations){ counter[Math.min(len,c)]++; } int k=len; for(int s=counter[len]; k > s; s += counter[k]){ k--; } return k; } |
My solution in Python3:
def get_H_index(citations):
citiations.sort(reverse=True)
n = len(citations)
h_index = 0
for i in range(n):
if citations[i]>i:
h_index += 1
return h_index