class Solution:
def hIndex(self, citations: List[int]) -> int:
"""
Calculate the h-index from a list of citation counts.
The h-index is defined as the maximum value h such that the researcher has published
at least h papers with each having at least h citations.
Parameters:
citations (List[int]): A list where each element represents the citation count for a paper.
Returns:
int: The h-index for the given citation counts.
"""
# First, sort the citation counts in ascending order.
citations.sort()
n = len(citations) # Total number of papers.
# Iterate over the sorted citations list.
# The idea is to find the smallest index i such that the (n - i) papers have at least (n - i) citations.
for i in range(n):
h = n - i # h is the number of papers with citation counts >= current paper's count.
# Check if the current paper's citation count is at least h.
if citations[i] >= h:
return h # This is the h-index.
# If no such h is found, return 0.
return 0
Summary of Techniques and Approaches:
Sorting Approach: Sorting the citations simplifies the problem by lining up the papers so that we can calculate the number of papers with citations above a threshold with a simple index arithmetic.
Single Pass Post-Sort: By iterating over the sorted list, we determine the candidate h-index in one pass, making the solution efficient with a time complexity of O(n log n) due to sorting and O(n) for the iteration.
Problem Breakdown: The problem is decomposed into sorting and then checking a condition with a transformed index (n - i). This technique is applicable to other problems where the order of elements is critical and you need a threshold-based decision.
General Tips: