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. "
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.
Follow up to H-index:what if the citations
array is sorted in ascending order? Could you optimize your algorithm?
idea: Two-point search. Notice the boundary condition of the search.
1 classSolution {2 Public:3 intHindex (vector<int>&citations) {4 intleft =0, right = Citations.size ()-1;5 while(Left <=Right ) {6 intMid = left + (right-left)/2;7 if(Citations[mid] < citations.size ()-mid)8Left = mid +1;9 Else if(Mid > Left && citations[mid-1] >= citations.size ()-(Mid-1))Tenright = mid-1; One Else returnCitations.size ()-mid; A } - return 0; - } the};
H-index II--Leetcode