H-Index (Counting Sort)
Given an array of integers citations where citations[i] is the number of citations a researcher received for their ith paper, return the researcher's h-index.
Definition: A scientist has an index h if h of their n papers have at least h citations each, and the other n - h papers have no more than h citations each. If there are multiple possible values for h, the maximum one is taken as the h-index.
- n == citations.length
- 1 <= n <= 5 * 10⁴
- 0 <= citations[i] <= 1000
citations = [3,0,6,1,5]3citations = [1,3,1]1The H-Index is a metric that attempts to measure both the productivity and citation impact of the publications of a scientist or scholar. The goal is to find the largest number h such that the researcher has published h papers that have each been cited at least h times.
Counting Sort is a non-comparison sorting algorithm. While algorithms like Quick Sort compare elements (is A > B?), Counting Sort works like a tally sheet.
Instead of moving elements around, we create a specialized storage called a Frequency Array (or Buckets).
The Value-to-Index Mapping:
In this array, the index itself represents the value we are counting.
- If you have papers with 3 citations, you increment the counter at index 3.
- If you have a paper with 0 citations, you increment index 0.
- The array acts as a map where Key = Citation Count and Value = Number of Papers.
How it works:
1. The Tally: We scan the input array. For every number we see, we go to its corresponding "seat" in the Frequency Array and increment a counter.
2. The Storage: If the input is [1, 0, 1], our Frequency Array at index 1 will store "2" (because 1 appeared twice) and at index 0 will store "1".
3. The Reconstruction: To "sort", we simply walk through the Frequency Array. If index 0 has a tally of 1, we write "0" once. If index 1 has a tally of 2, we write "1" twice.
The catch: You must know the range of values beforehand. If your numbers range from 0 to 1,000, you need 1,001 buckets. This is why it is linear in time but only used when values are bounded.
In the H-Index problem, we have a magical bound: the H-index cannot be larger than the number of papers (N). Even if a paper has 10,000 citations, it only helps us reach an H-index of at most N.
This allows us to create a Frequency Array of size N+1. We don't sort the papers by their citations; we store the count of papers for each possible impact level (0 citations, 1 citation... up to N+ citations).
> Technical Note: This is a Counting Sort. Since we only need to keep a simple tally (an integer) of how many papers have a certain citation count, we don't need to store the papers themselves in the buckets.
Since the maximum possible answer is n, we don't need to know the exact value of any citation count greater than n. They all just count as "high impact" papers.
n + 1. Each index i will store the count of papers that have exactly i citations.citations > n, we simply increment buckets[n].n down to 0. We maintain a running sum of papers encountered so far. The first value h where total_papers >= h is our H-index.def hIndex(citations):
n = len(citations)
buckets = [0] * (n + 1)
for c in citations:
if c >= n:
buckets[n] += 1
else:
buckets[c] += 1
# Sweep from highest possible h-index downwards
papers_seen = 0
for h in range(n, -1, -1):
papers_seen += buckets[h]
if papers_seen >= h:
return h
return 0Visual Intuition
Watch the counting sort process step-by-step.
Goal: Find largest H where H papers have >= H citations. Phase 1: Count citation frequencies.