H-Index

Medium

Topics
ArraySortingCounting Sort

Given an array citations where citations[i] is the number of citations for the i-th paper, return the researcher's h-index: the maximum h such that h papers have at least h citations each.

Example 1

3001621354
the h papers
The h-index is the largest h such that h papers each have at least h citations. Sorted descending, that is the last position where the value still reaches its own 1-based rank. Here h = 3, and the shaded cells are the papers that qualify.
Cells are the array values; the small numbers underneath are indices.
Input:  citations = [3,0,6,1,5]
Output: 3

Example 2

103112
the h papers
Input:  citations = [1,3,1]
Output: 1

Constraints

  • n == citations.length
  • 1 <= n <= 5000
  • 0 <= citations[i] <= 1000
Editorial — intuition, approach, complexity and pitfalls

Intuition

The h-index is the largest h such that at least h papers have h or more citations. Checking every candidate h against the whole array is O(n^2).

Sorting descending makes it one pass. After sorting, the paper at index i is the (i+1)-th most cited, so "at least i+1 papers have at least citations[i] citations" holds automatically. The condition to test is therefore just citations[i] >= i + 1, and the answer is the largest i+1 for which it holds.

Once the condition fails it can never hold again — citations are descending while the index requirement rises — so the loop can break immediately.

There is an O(n) alternative worth knowing: the h-index never exceeds n, so bucket the citation counts into n+1 slots (capping at n) and walk down accumulating a running total. That is counting sort applied to the same argument.

Approach

  1. Sort descending.
  2. Walk until citations[i] < i + 1, tracking the last i+1 that satisfied it.

Dry Run

citations = [3,0,6,1,5]
sorted desc [6,5,3,1,0]

i=0  6 >= 1   h = 1
i=1  5 >= 2   h = 2
i=2  3 >= 3   h = 3
i=3  1 >= 4?  no  -> break

answer 3
check: three papers (6, 5, 3) have at least 3 citations,
and no four papers have at least 4  ✓

Complexity

  • Time — O(n log n): the sort dominates; the scan is linear. The counting-sort variant is O(n).
  • Space — O(log n) in Java and C++, O(n) in Python, all of it the sort — an in-place quicksort recurses, Python's Timsort allocates a merge buffer. The scan itself holds one counter.

Common Pitfalls

  • Sorting ascending and then reasoning about n - i, which is correct but easier to get wrong by one. If you do, the condition is citations[i] >= n - i.
  • Comparing against i rather than i + 1. Indices are zero-based and the count of papers is not.
  • Returning the citation value instead of the count of papers.
  • Claiming O(1) space while sorting.
  • Continuing past the first failure. Harmless, but the break is what makes the early exit explicit.

Full Solution — all languages

class Solution:
    def hIndex(self, nums):
        nums.sort(reverse=True)
        h = 0
        for i, c in enumerate(nums):
            if c >= i + 1:
                h = i + 1
            else:
                break
        return h
SensAI
Beta
Tap the mic to stop voice mode