“This is the 10th day of my participation in the First Challenge 2022. For details: First Challenge 2022”
[B] [C] [D]
You are given an integer array of citations, where citations[I] represents the number of citations of the researcher’s i-th paper. Calculate and return the researcher’s ** H index.
The definition of h-index: H stands for “high citations”. A researcher’s H-index means that h of his or her papers have been cited at least H times. Each of the other N-H papers was cited no more than H times.
For example, if someone has an H-index of 20, that means they have 20 published papers that have been cited at least 20 times.
Tip: if h has multiple possible values, the h index ** is the largest of them.
Example 1:
Input: citations = [3,0,6,1,5] Output: 3 Explanation: The given array means that the researcher has a total of 5 papers, and each paper has been cited 3,0,6,1,5 times accordingly. Since three of the researchers' papers were cited at least three times each, and the other two papers were cited no more than three times each, her H-index was 3.Copy the code
Example 2:
Citations = [1,3,1] Output: 1Copy the code
Tip:
n == citations.length
1 <= n <= 5000
0 <= citations[i] <= 1000
Their thinking
This is not a difficult question, but it is a little difficult to understand the meaning of the question. If there are h papers that have been cited h or more times, then this h is a legitimate H, and the h-exponent is the largest of all h values that satisfy this condition. So how do we solve this problem? [0,1,3,5,6] [0,1,3,5,6] Let’s move on. At this point we scan the sorted array from back to front and record the current h value. At the beginning, we defined h = 1, at the end of the array.
1
[0,1,3,5,6]
Copy the code
Next, we judge whether the value of the current position of the array (that is, the number of references) is greater than or equal to the current H. If the condition is met, it means that we have found H papers and their references are greater than or equal to H, then the H value at this time is a legal H value. And then we go forward, and when we find an illegal position, we can exit the loop, and the legal h that we find at the end is the maximum legal H, which is the h power that we want.
Code implementation
Var hIndex = function (citations) {var hIndex = function (citations) {var hIndex = function (citations) {sort(a, b) => citations. Sort (a, b) => citations. H = 1 // When the number of citations in the current position is greater than or equal to the number of subsequent papers // The current h is valid (the number of citations in the current position and all subsequent papers are greater than h, and the current position and subsequent papers are H) while (citations[len) -h] >= h) h++ // Return h-1}Copy the code
At this point we are done with the Leetcode-274-H index
If you have any questions or suggestions, please leave a comment! 👏 🏻 👏 🏻 👏 🏻