Algorithm

H-Index (Counting Sort)

Sorting Pattern

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.

CONSTRAINTS
  • n == citations.length
  • 1 <= n <= 5 * 10⁴
  • 0 <= citations[i] <= 1000
EXAMPLE 1
Input: citations = [3,0,6,1,5]
Output: 3
The researcher has 5 papers with 3, 0, 6, 1, 5 citations. They have 3 papers with at least 3 citations (3, 5, 6) and the other 2 papers have no more than 3 citations. Thus, the h-index is 3.
EXAMPLE 2
Input: citations = [1,3,1]
Output: 1
There is 1 paper with at least 1 citation. There aren't 2 papers with at least 2 citations. Thus, the h-index is 1.
Can the researcher have an H-index of 0?
Yes. If all papers have 0 citations, the H-index is 0.
Is the input array guaranteed to be sorted?
No, the citations can appear in any order. If they were sorted, a linear scan or binary search would be even faster, but we must handle unsorted data.
How should I handle citation counts that exceed the number of papers?
A paper with 100 citations contributes the same to an H-index of a researcher with 5 papers as a paper with 5 citations. You can treat all counts >= n as being equal to n.
The Problem: Measuring Impact

The 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.

The Logic: What is Counting Sort?

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.

Why it works for H-Index

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.

The Counting Insight: Bounded Range

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.

1. The Buckets: Create a "buckets" array of size n + 1. Each index i will store the count of papers that have exactly i citations.
2. The Overflow: For any paper with citations > n, we simply increment buckets[n].
3. The Reverse Accumulation: Iterate backwards from 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.
python
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 0
Interactive Strategy Visualization

Visual Intuition

Watch the counting sort process step-by-step.

3
0
0
1
6
2
1
3
5
4
STEP 1 / 10 • INIT

Goal: Find largest H where H papers have >= H citations. Phase 1: Count citation frequencies.

O(N log N) Sorting -> O(N) Counting Sort