H-index I, II

Source: Internet
Author: User

I. Given an array of citations (each citation was 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 has at least H citations each, and the other n−h papers has no more than H citation S 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 papers with at least citations each and the remaining both with 3 3 no more than 3 CIT Ations each, he h-index is 3 .

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

Runtime:4ms.

1 classSolution {2  Public:3     intHindex (vector<int>&citations) {4         intn =citations.size ();5         if(n = =0)return 0;6         7         inti =1;8 sort (Citations.begin (), Citations.end ());9          while(I <=N) {Ten              while(Citations[n-i] >=i) Onei++; A             returnI1; -         } -         returncitations[0]; the     } -};

II. Follow up for h-index:what if the citations array was sorted in ascending order? Could you optimize your algorithm?

Runtime:12ms.

1 classSolution {2  Public:3     intHindex (vector<int>&citations) {4         intn =citations.size ();5         if(n = =0)return 0;6         7         intLow =0, high = n-1;8          while(Low <=High ) {9             intMid = (low + high)/2;Ten              One             if(N-mid <=Citations[mid]) AHigh = mid-1; -             Else -Low = mid +1; the         } -         returnN-Low ; -     } -};

H-index I, II

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.