H-Index II

Find a researcher's h-index from citation counts sorted from smallest to largest, without looking at every paper.

Given an array citations, where citations[i] is how many times a researcher's paper number i was cited, sorted in ascending order, return the researcher's h-index. The h-index is the largest number h such that the researcher has at least h papers that were each cited at least h times. The method should not look at every paper: it should take O(log n) time, where n is the number of papers.

Example 1
Input: citations = [0, 1, 3, 5, 6]
Output: 3
Explanation: three papers (with 3, 5 and 6 citations) have at least 3 citations each, but there are not four papers with at least 4.
Example 2
Input: citations = [1, 2, 100]
Output: 2
Solution

The Idea

When a paper mentions another paper as a source, that mention is called a citation. Counting all citations rewards a researcher with one famous paper. The h-index asks for something steadier: many papers that are each cited often.

Take the researcher from Example 1, with five papers cited 0, 1, 3, 5 and 6 times. Is the h-index 3? The papers with at least 3 citations are the ones with 3, 5 and 6, which makes three papers, so yes. Is it 4? Only the papers with 5 and 6 citations reach 4, which is two papers, not four. So the answer is 3.

Trying every possible h like this means counting papers again and again. How can the sorted order save that work? The most-cited papers sit at the right end. Start at any index i and look right: the papers from i to the end are the n - i most-cited papers, and the one at i has the fewest citations among them.

So one comparison answers a whole question. If citations[i] >= n - i, then all n - i of those papers have at least n - i citations, and the h-index is at least n - i. Look at the picture below, which does this for every index of Example 1.

The sorted citations 0, 1, 3, 5, 6 at indexes 0 to 4. Under each, the number of papers from there to the end: 5, 4, 3, 2, 1. The answer to enough citations is no, no, yes, yes, yes. The first yes is at index 2, where 3 papers have at least 3 citations, so h = 3.

The row labelled "papers from here to the end" counts down 5, 4, 3, 2, 1. The row labelled "enough citations?" compares it with the citations above. At index 1, there are 4 papers, but the weakest has only 1 citation, so the answer is no. At index 2, there are 3 papers, and the weakest has 3 citations, so the answer is yes.

Notice the shape of that last row: no, no, then yes until the end. Moving right, the citations only grow and the paper count only shrinks, so once the answer is yes it stays yes. The first yes gives the biggest paper count that passes, and that count is the h-index: 3 papers with at least 3 citations, h = 3.

So the problem becomes: find the index where no turns into yes. A row that is all no on the left and all yes on the right can be searched by checking its middle and throwing away the half that cannot hold the answer. That way of searching is called binary search.

Solution Approach

Turning that idea into code means settling three things: how to find the first yes quickly, what to do when no index says yes, and what to return once the first yes is found.

Finding the first yes

Why not check the indexes from left to right and stop at the first yes? That works, but for a researcher with a million papers it can take a million checks, and the problem asks for fewer.

Binary search instead keeps a range, from lo to hi, that is known to hold the first yes, and checks the index mid in its middle. A yes at mid means the first yes is at mid or to its left, so hi moves to mid. A no means every index up to mid says no, so lo moves to mid + 1.

In Example 1, the range starts as 0 to 5. Index 5 is one place past the last paper, kept in case no index says yes. The middle is index 2, which says yes, so the range shrinks to 0 to 2. The middle is now index 1, which says no, so the range becomes 2 to 2. The two ends meet at index 2 after only two checks.

When no index says yes

What if no paper passes? Take [0, 0]: index 0 has 2 papers but 0 citations, and index 1 has 1 paper but 0 citations. Both say no. For this case hi starts at n, one place past the last paper, which stands for "no index says yes". The search then ends at 2, and the paper count there is 2 - 2 = 0, which is the right h-index.

Returning a count

Once lo sits on the first yes, what is the answer? It is the number of papers from lo to the end, n - lo. The h-index counts papers, so it is never more than n, however many citations those papers have.

Java Solution

The method hIndex works in these steps:

  1. First, store the number of papers in n. Set lo to 0 and hi to n, so the range from lo to hi covers every index plus the "no index says yes" position.
  2. While lo is less than hi, take the middle index mid and check citations[mid] >= n - mid.
  3. On a yes, move hi to mid. On a no, move lo to mid + 1.
  4. When the loop stops, lo and hi meet at the first yes. Return n - lo, the number of papers from there to the end.

The main method runs both examples, two papers with no citations, one paper with 100 citations, five papers with 5 citations each, and [0, 1, 4, 5, 6].

Java
public class HIndexII {

    public static int hIndex(int[] citations) {
        int n = citations.length;
        int lo = 0;
        int hi = n;                     // n stands for "no paper passes the test"
        while (lo < hi) {
            int mid = lo + (hi - lo) / 2;
            if (citations[mid] >= n - mid) {
                hi = mid;               // mid passes: the first pass is at mid or before it
            } else {
                lo = mid + 1;           // mid fails: the first pass is after it
            }
        }
        return n - lo;                  // the number of papers from lo to the end
    }

    public static void main(String[] args) {
        System.out.println(hIndex(new int[] {0, 1, 3, 5, 6}));
        System.out.println(hIndex(new int[] {1, 2, 100}));
        System.out.println(hIndex(new int[] {0, 0}));
        System.out.println(hIndex(new int[] {100}));
        System.out.println(hIndex(new int[] {5, 5, 5, 5, 5}));
        System.out.println(hIndex(new int[] {0, 1, 4, 5, 6}));
    }
}

Running it prints:

Output
3
2
0
1
5
3

The 3 and the 2 at the top match Examples 1 and 2. In [1, 2, 100], the paper with 100 citations does not lift the answer above 2, because only two papers have at least 2 citations. The 0 on the third line comes from [0, 0], where the search ends on the "no index says yes" position.

The 1 on the fourth line is the single paper with 100 citations: one paper can give an h-index of at most 1. The 5 below it comes from five papers with 5 citations each, where index 0 already says yes and all five papers count.

The last line, 3 for [0, 1, 4, 5, 6], shows why the method returns n - lo and not citations[lo]. In Example 1 both are 3, so either seems to work. Here the first yes is still index 2, but citations[2] is 4. A version returning citations[lo] prints 4 for this array, and 100 for [100], while only 3 papers and 1 paper really count.

Complexity

The work is one binary search over the n + 1 positions from 0 to n.

  • Time: O(log n), because each check of citations[mid] >= n - mid halves the range from lo to hi, so about log₂(n + 1) checks find the first yes. A million papers need about 20 checks.
  • Space: O(1), because the method keeps only n, lo, hi and mid, and reads the sorted array without copying it.

If the citations were not sorted, sorting them first would cost O(n log n) time before the same search.