Leetcode Note: H-Index
I. Description
Given an array of citations (each citation is 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 have at least h citations each, and the other N? H papers have no more than h citations each ."
For example, givencitations = [3, 0, 6, 1, 5]
, Which means the researcher has 5 papers in total and each of them had stored ed3, 0, 6, 1, 5
Citations respectively. Since the researcher has 3 papers with at least 3 citations each and the remaining two with no more than 3 citations each, his h-index is 3.
Note: If there are several possible values for h, the maximum one is taken as the h-index.
Ii. Question Analysis
First, you need to understand the general idea of the question:
Given an array that records the number of times a researcher references a document (each document references a non-negative integer), write a function to calculate the h index of the investigator.
According to Wikipedia's definition of the h index: "A scientist's h index is published inN
In this paperh
The papers are referenced at leasth
Times, OtherN-h
No moreh
Times ".
For example, given an arraycitations = [3, 0, 6, 1, 5]
, Which means that the researchers have a total5
Each article has obtained3, 0, 6, 1, 5
. Since researchers have3
The papers have at least obtained3
The number of references in the other two articles does not exceed3
Times, so its h index is3
.
NOTE: If there are multiple possibleh
Value, take the maximum valueh
Index.
You can learn moreh
The definition of the value, which is in the lower left corner of the ball.SquareMaximum:
In the following explanation, assume that the size of the given array isN
, That is, the totalN
Article.
There are two common practices, which are also mentioned in the topic tips. The first thing we think of is to sort the array and traverse from the back to find out the h value. The complexity of this method is:O(n*logn)
.
During the interview, if secondary memory is allowed, you can use the second method, that is, opening up a new arrayrecord
For record0~N
The number of times each times has several articles.N
AccordingN
Count) traverse the array. After counting, traverse the array once.RecZ records? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vcmQ8L2NvZGU + o6y8tL/Jy + weight + yP0uIMq + wP20 + sLrPC9zdHJvbmc + Weight =" brush: java; ">// Sort + traverse class Solution {public: int hIndex (vector & 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 ;}};
// Method 2 class Solution {public: int hIndex (vector & Citations) {int citationSize = citations. size (); if (citationSize <1) return 0; vector Record (citationSize + 1, 0); for (int I = 0; I <citationSize; ++ I) {if (citations [I] <= citationSize) ++ record [citations [I]; else ++ record [citationSize];} for (int j = citationSize, paperNum = 0; j> = 0; -- j) {paperNum + = record [j]; if (paperNum> = j) return j;} return 0 ;}};
Iv. Summary
The method used depends on the actual conditions.