H-Index II (LeetCode 275): Solution and Explanation

Medium
Array
Binary Search

The citations array is sorted in ascending order. Return the largest h such that at least h papers have at least h citations each, in logarithmic time.

Intuition

Because the array is sorted, the papers from index i to the end are the n - i most cited ones, and the weakest of them has citations[i] citations.

So h = n - i is achievable exactly when citations[i] >= n - i. Every one of those n - i papers has at least citations[i] citations.

Moving i right makes citations[i] bigger and n - i smaller, so the test is false for a prefix of indices and true for the rest. That shape is what binary search finds.

The first index lo where the test holds gives the largest h, which is n - lo. If no index passes, lo ends at n and the answer is 0.

Approach: Binary search for the first passing index

  1. Set lo = 0 and hi = n - 1.
  2. While lo <= hi, take mid = (lo + hi) // 2.
  3. If citations[mid] >= n - mid, then mid passes; a smaller index might pass too, so set hi = mid - 1.
  4. Otherwise mid fails, and so does everything left of it; set lo = mid + 1.
  5. Return n - lo.

Step-by-step example

citations = [0,1,3,5,6]
  1. n = 5, lo = 0, hi = 4.
  2. mid = 2: citations[2] = 3 and n - mid = 3. 3 >= 3 passes, so hi = 1.
  3. mid = 0: citations[0] = 0 and n - mid = 5. 0 < 5 fails, so lo = 1.
  4. mid = 1: citations[1] = 1 and n - mid = 4. 1 < 4 fails, so lo = 2.
  5. lo > hi, stop. The first passing index is 2, so the h-index is 5 - 2 = 3.

Code: Python, Java, C++

1class Solution:
2    def hIndex(self, citations: List[int]) -> int:
3        n = len(citations)
4        lo, hi = 0, n - 1
5        while lo <= hi:
6            mid = (lo + hi) // 2
7            if citations[mid] >= n - mid:
8                hi = mid - 1
9            else:
10                lo = mid + 1
11        return n - lo
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Binary search for the first passing index O(log n)
Binary search on h O(log n)
Linear scan O(n)
Time

O(log n)

Each comparison halves the range of indices still in play.

Space

O(1)

Only lo, hi and mid are stored.

Other approaches

Linear scan (time O(n), space O(1))

Walk i from 0 to n - 1 and return n - i at the first index where citations[i] >= n - i. Correct and simple, but O(n), which the problem rules out.

Binary search on h (time O(log n), space O(1))

Binary search the value h in [0, n] and check citations[n - h] >= h for each candidate. It is the same test seen from the other side.

Common mistakes

  • Returning citations[lo] instead of n - lo. The h-index is a count of papers, not a citation number.
  • Comparing against mid or mid + 1 instead of n - mid. The candidate h for index mid is the number of papers from mid to the end.
  • Stopping early on equality. Equality does pass, but a smaller index may also pass and give a larger h, so keep searching left.
  • Forgetting the all-zero case, where no index passes and the answer must be 0.

Frequently asked questions

How is this different from H-Index (LeetCode 274)?

In 274 the array is unsorted, so you sort it or count citations into buckets. Here it is already sorted, which allows the O(log n) binary search.

Why is the test monotone?

citations[i] never decreases and n - i strictly decreases as i grows. Once citations[i] >= n - i is true, it stays true for every later index.

Can the h-index be larger than n?

No. h counts papers, so it is at most n even if every paper has thousands of citations.