Solution: H-Index
Let’s solve the H-Index problem using the Sort and Search pattern.
We'll cover the following
Statement
You are given an array of integers citations
, where each element citations[i]
represents the number of citations received for the
Your task is to find the researcher’s h-index and return the value of
Note: The h-index is defined as the highest number
such that the given researcher has published at least papers, each of which has been cited at least times.
Constraints:
citations.length
citations[i]
Solution
The core idea of this solution is to calculate the h-index of a researcher by using a counting sort-based strategy rather than sorting the citation
array directly. Since the h-index is defined as the maximum value
The algorithm builds a bucket array to count how many papers have exactly citations
, where any citation value above
This approach avoids the overhead of full sorting and leverages counting sort principles along with a reverse accumulation search.
Now, let’s look at the solution steps below:
We initialize the following:
n
to represent the number of papers (length of thecitations
list).A list
papers
of sizen + 1
to count how many papers have a specific number of citations, withpapers[i]
representing the count of papers cited exactlyi
times.All citation values greater than
n
are capped and grouped intopapers[n]
, since the h-index cannot exceedn
.
Next, we iterate through the
citations
list:For each citation count,
c
, we incrementpapers[min(n, c)]
.
We also initialize
k = n
as our candidate h-index, ands = papers[n]
, the number of papers with at leastn
citations.We then iterate through the
papers
array, whilek > s
, and search for the h-index by:We decrement
k
and addpapers[k]
tos
, computing the total number of papers with at leastk
citations.
The iteration continues until we find the highest
k
such that there are at leastk
papers withk
or more citations.After iterating, the current
k
is returned as the h-index.
Let’s look at the following illustration to get a better understanding of the solution:
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.