H-Index II
LeetCode Problem #275 (Medium)
Last updated
Was this helpful?
LeetCode Problem #275 (Medium)
Last updated
Was this helpful?
Given an array of citations sorted in ascending order (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.
According to the : "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."
In other words, we must find the first element such that the value of that element is greater than or equal to the number of elements remaining in the array. We may not find an element in the array with such a value, but we can calculate the h-index value as len - mid
.
The reason for this is because we always need to find a value h
wherein, there are at least h
number of values from the array that are the greater than or equal to h
. Calculating len - mid
will give us the optimized version of this value.
There may also be more than one value which satisfies the condition, but we are interested in the maximum value among these values.
Refer to the Prerequisite Knowledge section in Random Pick with Weight:
In this approach, we need to traverse through the array to find the value of the first array element that is greater than or equal to the number of elements that remain in the array. The conventional approach would be to traverse the array in linear time, but since the array has already been sorted, we can make use of binary search to achieve the same result.
Initialize the high and low pointers to point to the extreme ends of the sorted array.
Calculate the mid-value.
Check if the condition to find the h-index is satisfied by the mid element:
citations[index] >= length(citations) - index
Store the value of the remaining elements in a variable and continue the process to find the optimum value.
When the first optimal value that satisfies the condition is found, return the value.
Example :
The mid-value is currently 3. There must be at least 3 papers with a value greater than or equal to 3.
len - mid = 5 - 2 = 3
3 >= 3 -> true
h_index = len - mid = 5 - 2 = 3
There are 3 (h)
papers that have an h-index of at least 3 and 2 (N-h)
papers that have an h-index of no more than 3. This satisfies the stipulations of the given problem.