H-Index (LeetCode 274): Solution and Explanation
Each entry of citations is how often one paper was cited. Return the largest h such that at least h papers have at least h citations each.
Intuition
The answer is a count of papers, so it lies between 0 and n. A brute force tries every h from n down to 0 and counts how many papers have at least h citations. That is O(n²), and it hides a simpler structure.
For a fixed h, the best papers to support it are obviously the h most cited ones. If even those fail, no other choice of h papers can succeed. After sorting ascending, those h papers are the last h entries, starting at index n - h.
Within that group only the weakest paper can break the claim. If citations[n - h] >= h, every paper to its right is at least as large, so all h of them qualify. That turns each candidate check into a single comparison.
Writing i = n - h, the check becomes citations[i] >= n - i. Walking i from 0 upward tries h = n, n - 1, ... in decreasing order, so the first index that passes gives the largest valid h.
Approach: Sort and scan for the first passing index
- Sort
citationsin ascending order and letnbe its length. - For
ifrom 0 to n - 1, the candidate ish = n - i: the papers at indicesi..n-1. - If
citations[i] >= n - i, the weakest paper of the group clears the bar, so returnn - i. - Otherwise paper
iis too weak for this h. Move toi + 1, which lowers h by one and drops that paper from the group. - If no index passes, every paper has fewer citations than its candidate, so return 0.
Step-by-step example
citations = [3,0,6,1,5]
- Sort to get [0,1,3,5,6], n = 5.
- i = 0, h = 5: the weakest of all five papers has 0 citations, and 0 < 5, so fail.
- i = 1, h = 4: the weakest of the top four has 1 citation, and 1 < 4, so fail.
- i = 2, h = 3: the weakest of the top three has 3 citations, and 3 >= 3, so pass.
- Return 3. Larger values of h already failed, so 3 is the h-index.
Code: Python, Java, C++
1class Solution:
2 def hIndex(self, citations: List[int]) -> int:
3 citations.sort()
4 n = len(citations)
5 for i in range(n):
6 if citations[i] >= n - i:
7 return n - i
8 return 01class Solution {
2 public int hIndex(int[] citations) {
3 Arrays.sort(citations);
4 int n = citations.length;
5 for (int i = 0; i < n; i++) {
6 if (citations[i] >= n - i) {
7 return n - i;
8 }
9 }
10 return 0;
11 }
12}1class Solution {
2public:
3 int hIndex(vector<int>& citations) {
4 sort(citations.begin(), citations.end());
5 int n = citations.size();
6 for (int i = 0; i < n; i++) {
7 if (citations[i] >= n - i) {
8 return n - i;
9 }
10 }
11 return 0;
12 }
13};Time and space complexity
O(n log n)
Sorting dominates. The scan afterwards is a single pass with one comparison per index.
O(1)
The sort is done in place (ignoring the sort's own stack) and the scan uses a couple of integers.
Other approaches
Counting buckets (time O(n), space O(n))
Any citation count above n can be capped at n, so count papers into buckets 0..n. Walk the buckets from n down while keeping a running total of papers seen; the first bucket where the total reaches the bucket index is the answer. This avoids sorting.
Try every h (time O(n²), space O(1))
For each h from n down to 0, count the papers with at least h citations and return the first h whose count is at least h. Simple and correct, but quadratic.
Common mistakes
- Returning
citations[i]instead ofn - i. The h-index counts papers, so it is the group size, not a citation value. For [25,8,5,3,3] the answer is 3, not 5. - Using
>instead of>=. A paper with exactly h citations does count toward h. - Forgetting the all-zero case. If no index passes, the loop ends and the answer is 0.
- Sorting descending and then mixing up the index formula. Pick one order and derive the comparison from it.
Frequently asked questions
Why is checking only citations[i] enough?
After an ascending sort, every paper to the right of i has at least citations[i] citations. If the smallest one in the group reaches h, the rest do too.
Why does the first passing index give the largest h?
As i increases, h = n - i decreases. Scanning from i = 0 tries candidates from largest to smallest, so the first one that works is the maximum.
Can the h-index be larger than n?
No. It counts papers, and there are only n of them. That is also why citation counts above n can be capped in the counting version.
How does H-Index II differ?
There the array is already sorted, so you can binary search for the first index with citations[i] >= n - i and get O(log n).