I. Title Description
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 having at least h CIT Ations, and the other N? H Papers has no more than H citations each. "
For example, given citations = [3, 0, 6, 1, 5]
, which means the researcher have 5 papers in total and each of the them had received 3, 0, 6, 1, 5
citations R Espectively. Since the researcher have 3 papers with at least 3 citations each and the remaining both with no more than 3 citations each, His h-index is 3.
Note:if there was several possible values for H, and the maximum one is taken as the h-index.
Two. Topic analysis
First you need to understand the main idea of the topic:
Given an array, the number of citations of a researcher is documented (the number of citations for each article is a nonnegative integer), and the writing function calculates the H index of the researcher.
According to Wikipedia's definition of the H index: "The H index of a scientist means that in his published N
paper, a paper is h
quoted at least h
once, and N-h
the number of citations for the remainder of the article is no more than h
".
For example, given an array citations = [3, 0, 6, 1, 5]
, this means that the researcher has a total of 5
papers, each of which gets a 3, 0, 6, 1, 5
secondary reference. Since the researchers had 3
at least one citation, the 3
other two citations did not exceed the number of 3
times, so the H index was 3
.
Note: If there are more than one possible h
value, take the maximum value as an h
exponent.
Through, you can more intuitively understand the h
definition of the value, corresponding to the graph, that is, the lower left corner of the ball square the maximum value:
The following explanation assumes that the size of the given array is N
, that is, a total N
article.
There are two general practices, also referred to in the topic tips, the first thought is to sort the array, and then traverse from the back to find the H value, the complexity of the method is: O(n*logn)
.
In the interview, if you allow the use of secondary memory, you can use the second method, that is, to open up a new array record
, to record the 0~N
number of times cited several articles (the number of references greater than N
the N
second calculation) to iterate over the array, after the statistics, to iterate over the statistical array record
, h
the maximum value of the value can be calculated. The complexity of time is O(n)
.
Three. Sample code
//sort + traverse class Solution {public : int hindex (vector <int > & citations) {sort (Citations.begin (), Citations.end (), [] (const int &a, const int &b) {return a > B;}); int i = 0 ; for (; i < citations.size (); ++i) if (Citations[i] <= i) break ; return i; }};
//The second methodclassSolution { Public:intHindex ( vector<int>& citations) {intCitationsize = Citations.size ();if(Citationsize <1)return 0; vector<int>Record (Citationsize +1,0); for(inti =0; i < citationsize; ++i) {if(Citations[i] <= citationsize) ++record[citations[i];Else++record[citationsize]; } for(intj = citationsize, Papernum =0; J >=0; --J) {papernum + = Record[j];if(Papernum >= J)returnJ }return 0; }};
Four. Summary
Which method to use depends on the actual conditions.
Leetcode notes: H-index