(Java) Leetcode 274. H-index--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 H citation S each. "

Example:

Input:citations = [3,0,6,1,5]output:3 explanation: [3,0,6,1,5] means the researcher have 5 papers in total and each of th EM had              received 3, 0, 6, 1, 5 citations respectively.              Since the researcher have 3 papers with at least 3 citations each and the remaining, with              no more than 3 citations E Ach, her h-index is 3.

Note:if there was several possible values for H, and the maximum one is taken as the h-index.

Solution One, O (nlogn):

The reason for this is obvious, because the relationship between the number of references and the number of articles is established after the order is ordered. For example, after the order has been ordered, it becomes {0, 1, 3, 5, 6}. If you reverse-iterate over the sorted array, you will get the following information: The fifth article was cited 6 times, proving that there are five articles cited less than or equal to it, in other words, there is an article cited more than or equal to 5. The H-index requires that H-articles be quoted at least H, which proves that this reference is too large and continues to traverse in a less-referenced direction. When traversing to the fourth article, the number of citations is 5 times, proving that there are two articles with a reference number greater than or equal to 5. Continue, the third article is cited three times, then there are three articles cited more than or equal to 3 times. And then continue to traverse the time, know that there are four articles with a reference number greater than or equal to 1, has not satisfied the condition. So in fact, for example, the H index has been calculated, that is, 3, that is, the result appears in between satisfying and not satisfying the definition of the traversal. Consider the special case, if the input is {3, 3, 3, 5, 6}? Traversing to the third article when I learned that at least three articles cited more than or equal to 3, and by the time of the second article 4 articles cited more than or equal to the 3,h index is larger than the definition. So when the number of articles reaches the threshold for the first time, the H-index should not be increased. The code is strictly greater than or greater than or equal to the number. If you do not understand, you can think of an example of {0, 0, 0} or {1, 1, 1}. See the code below.

Solution Two, O (n):

The purpose of a sort of solution is to establish the relationship between the number of references and the number of articles. Is there a way to do this without sorting, or at least not by comparing the sort to establish the relationship? Because the time complexity of comparing sorting is at least linear logarithm, non-comparison ordering can be optimized to linear time complexity. This is the time to think about counting sort. By counting, directly count the number of articles below each citation count. And then by scanning the accumulation, you will know more than equal to a certain number of citations of the article is how much. When a count of the number of the first article is greater than or equal to the number of references, the request is found. It is important to note that if an article has a reference number greater than or equal to the total number of articles, its contribution to the H index is at most n times, that is, the total number of articles. Because the H index is unlikely to be greater than the total number of articles. So at the time of counting, put this kind of article on the last side of the Count array, plus the special judgment. See the code.

Solution One (Java)

 class   solution { public  int  hindex (int  Span style= "COLOR: #000000" >[] citations) { if  (citations = = null  | |        Citations.length = = 0) return  0;        Arrays.sort (citations);         int  res = 0;  for  (int  i = citations.length-1; I >= 0; I--)  if  (Citations[i] > Res) res        ++;     return   res; }}

Solution Two (Java)

classSolution { Public intHindex (int[] citations) {        intn =citations.length; int[] Count =New int[n + 1];  for(inti = 0; I < n; i++) {            if(Citations[i] >= N) count[n]++number of articles with more citations than the total number of articlesElsecount[citations[i]]++;//Count how many articles are under each citation count}if(Count[n] >= N)returnN;//If the number of references per article is more than the number of articles, then the H index can only be the number of articles n for(inti = n-1; I >= 0; i--) {Count[i]+ = Count[i + 1]; Do not forget that I has been the number of references here, so this step in the statistics is greater than equal to the total number of articles, that is, accumulate a count of greater than or equal to Iif(Count[i] >= i)returni;//Once the number of references that satisfy the condition is found, return}return0; }}

(Java) Leetcode 274. H-index--h Index

Related Article

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.