Leetcode H-index

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.