H-Index II (LeetCode 275): Solution and Explanation
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
- Set
lo = 0andhi = n - 1. - While
lo <= hi, takemid = (lo + hi) // 2. - If
citations[mid] >= n - mid, thenmidpasses; a smaller index might pass too, so sethi = mid - 1. - Otherwise
midfails, and so does everything left of it; setlo = mid + 1. - Return
n - lo.
Step-by-step example
citations = [0,1,3,5,6]
- n = 5, lo = 0, hi = 4.
- mid = 2: citations[2] = 3 and n - mid = 3. 3 >= 3 passes, so hi = 1.
- mid = 0: citations[0] = 0 and n - mid = 5. 0 < 5 fails, so lo = 1.
- mid = 1: citations[1] = 1 and n - mid = 4. 1 < 4 fails, so lo = 2.
- 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 - lo1class Solution {
2 public int hIndex(int[] citations) {
3 int n = citations.length;
4 int lo = 0, hi = n - 1;
5 while (lo <= hi) {
6 int mid = lo + (hi - lo) / 2;
7 if (citations[mid] >= n - mid) {
8 hi = mid - 1;
9 } else {
10 lo = mid + 1;
11 }
12 }
13 return n - lo;
14 }
15}1class Solution {
2public:
3 int hIndex(vector<int>& citations) {
4 int n = citations.size();
5 int lo = 0, hi = n - 1;
6 while (lo <= hi) {
7 int mid = lo + (hi - lo) / 2;
8 if (citations[mid] >= n - mid) {
9 hi = mid - 1;
10 } else {
11 lo = mid + 1;
12 }
13 }
14 return n - lo;
15 }
16};Time and space complexity
O(log n)
Each comparison halves the range of indices still in play.
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 ofn - lo. The h-index is a count of papers, not a citation number. - Comparing against
midormid + 1instead ofn - 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.