Given an array of citations (each citation was a non-negative integer) of a researcher, write a function to compute the RES Earcher ' s h-index.
According to the definition of H-index on Wikipedia: ' A scientist has index H if h of his/her N Papers has at least H citations each, and the other N? h Papers has no more than C7>h citations each. "
For example, given citations = [3, 0, 6, 1, 5]
, which means the researcher have papers in total and each of 5
them had received 3, 0, 6, 1, 5
Citatio NS respectively. Since the researcher have 3
papers with at least citations each and the remaining are with 3
no more t Han 3
citations each, he h-index is 3
.
Note: If There is several possible values h
for, the maximum one is taken as the h-index.
First sort citations, compare citations[i] and n-i, one is the number of references, and the other is the number of paper that have been quoted so many times at least. H-index should be the smaller of the two. And if citations[i] < N-i, then the next citations[i+1] should be checked to be satisfied with this condition.
1 classsolution (object):2 defHindex (Self, citations):3 """4 : Type Citations:list[int]5 : Rtype:int6 """7 if notcitations:8 return09 Citations.sort ()Tenn =Len (citations) OneH_index =0 A forIinchrange (n): -cur = min (citations[i], N-i) - ifCur >H_index: theH_index =cur - returnH_index
Or
1 defHindex (Self, citations):2 """3 : Type Citations:list[int]4 : Rtype:int5 """6 if notcitations:7 return08 Citations.sort ()9n =Len (citations)TenH_index =0 One forIinchrange (n): A ifCitations[i] < n-I: -H_index =Citations[i] - Else: the returnN-I - returnH_index
If the citations is already sorted out. The complexity of the search can be changed to O (LONGN) using the dichotomy method
1 defHindex (Self, citations):2 """3 : Type Citations:list[int]4 : Rtype:int5 """6 7 if notcitations:8 return09n =Len (citations)Tenleft =0 Oneright = N-1 A - whileLeft <=Right : -Mid = (left + right)//2 the ifCitations[mid] < n-Mid: -left = mid + 1 - Else: -right = Mid-1 + - returnN-left
L13 If this equals sign is not added, an error will occur for the citations list of length =1.
Leetcode H-index