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