H-Index (LeetCode 274): Solution and Explanation

Medium
Array
Sorting
Counting Sort

Each entry of citations is how often one paper was cited. Return the largest h such that at least h papers have at least h citations each.

Intuition

The answer is a count of papers, so it lies between 0 and n. A brute force tries every h from n down to 0 and counts how many papers have at least h citations. That is O(n²), and it hides a simpler structure.

For a fixed h, the best papers to support it are obviously the h most cited ones. If even those fail, no other choice of h papers can succeed. After sorting ascending, those h papers are the last h entries, starting at index n - h.

Within that group only the weakest paper can break the claim. If citations[n - h] >= h, every paper to its right is at least as large, so all h of them qualify. That turns each candidate check into a single comparison.

Writing i = n - h, the check becomes citations[i] >= n - i. Walking i from 0 upward tries h = n, n - 1, ... in decreasing order, so the first index that passes gives the largest valid h.

Approach: Sort and scan for the first passing index

  1. Sort citations in ascending order and let n be its length.
  2. For i from 0 to n - 1, the candidate is h = n - i: the papers at indices i..n-1.
  3. If citations[i] >= n - i, the weakest paper of the group clears the bar, so return n - i.
  4. Otherwise paper i is too weak for this h. Move to i + 1, which lowers h by one and drops that paper from the group.
  5. If no index passes, every paper has fewer citations than its candidate, so return 0.

Step-by-step example

citations = [3,0,6,1,5]
  1. Sort to get [0,1,3,5,6], n = 5.
  2. i = 0, h = 5: the weakest of all five papers has 0 citations, and 0 < 5, so fail.
  3. i = 1, h = 4: the weakest of the top four has 1 citation, and 1 < 4, so fail.
  4. i = 2, h = 3: the weakest of the top three has 3 citations, and 3 >= 3, so pass.
  5. Return 3. Larger values of h already failed, so 3 is the h-index.

Code: Python, Java, C++

1class Solution:
2    def hIndex(self, citations: List[int]) -> int:
3        citations.sort()
4        n = len(citations)
5        for i in range(n):
6            if citations[i] >= n - i:
7                return n - i
8        return 0
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Counting buckets O(n)
Sort and scan for the first passing index O(n log n)
Try every h O(n²)
Time

O(n log n)

Sorting dominates. The scan afterwards is a single pass with one comparison per index.

Space

O(1)

The sort is done in place (ignoring the sort's own stack) and the scan uses a couple of integers.

Other approaches

Counting buckets (time O(n), space O(n))

Any citation count above n can be capped at n, so count papers into buckets 0..n. Walk the buckets from n down while keeping a running total of papers seen; the first bucket where the total reaches the bucket index is the answer. This avoids sorting.

Try every h (time O(n²), space O(1))

For each h from n down to 0, count the papers with at least h citations and return the first h whose count is at least h. Simple and correct, but quadratic.

Common mistakes

  • Returning citations[i] instead of n - i. The h-index counts papers, so it is the group size, not a citation value. For [25,8,5,3,3] the answer is 3, not 5.
  • Using > instead of >=. A paper with exactly h citations does count toward h.
  • Forgetting the all-zero case. If no index passes, the loop ends and the answer is 0.
  • Sorting descending and then mixing up the index formula. Pick one order and derive the comparison from it.

Frequently asked questions

Why is checking only citations[i] enough?

After an ascending sort, every paper to the right of i has at least citations[i] citations. If the smallest one in the group reaches h, the rest do too.

Why does the first passing index give the largest h?

As i increases, h = n - i decreases. Scanning from i = 0 tries candidates from largest to smallest, so the first one that works is the maximum.

Can the h-index be larger than n?

No. It counts papers, and there are only n of them. That is also why citation counts above n can be capped in the counting version.

How does H-Index II differ?

There the array is already sorted, so you can binary search for the first index with citations[i] >= n - i and get O(log n).