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 citationscitations[i]ihhh
Example 1
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
Output: 2
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 hiin - ii
So one comparison answers a whole question. If citations[i] >= n - in - in - in - i
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 lohimidmidmidhimidmidlomid + 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]hin2 - 2 = 0
Returning a count
Once lolon - lon
Java Solution
The method hIndex
- First, store the number of papers in
. Setn to 0 andlo tohi , so the range fromn tolo covers every index plus the "no index says yes" position.hi - While
is less thanlo , take the middle indexhi and checkmid .citations[mid] >= n - mid - On a yes, move
tohi . On a no, movemid tolo .mid + 1 - When the loop stops,
andlo meet at the first yes. Returnhi , the number of papers from there to the end.n - lo
The main[0, 1, 4, 5, 6]
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:
3
2
0
1
5
3
The 3 and the 2 at the top match Examples 1 and 2. In [1, 2, 100][0, 0]
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]n - locitations[lo]citations[2]citations[lo][100]
Complexity
The work is one binary search over the n + 1 positions from 0 to n.
- Time: O(log n), because each check of
halves the range fromcitations[mid] >= n - mid tolo , so about log₂(n + 1) checks find the first yes. A million papers need about 20 checks.hi - Space: O(1), because the method keeps only
,n ,lo andhi , and reads the sorted array without copying it.mid
If the citations were not sorted, sorting them first would cost O(n log n) time before the same search.