H-Index
Medium
Topics
Given an array citations where citations[i] is the number of citations for the i-th paper, return the researcher's h-index: the maximum h such that h papers have at least h citations each.
Example 1
h such that h papers each have at least h citations. Sorted descending, that is the last position where the value still reaches its own 1-based rank. Here h = 3, and the shaded cells are the papers that qualify.Input: citations = [3,0,6,1,5] Output: 3
Example 2
Input: citations = [1,3,1] Output: 1
Constraints
- n == citations.length
- 1 <= n <= 5000
- 0 <= citations[i] <= 1000
Editorial — intuition, approach, complexity and pitfalls
Intuition
The h-index is the largest h such that at least h papers have h or more citations. Checking every candidate h against the whole array is O(n^2).
Sorting descending makes it one pass. After sorting, the paper at index i is the (i+1)-th most cited, so "at least i+1 papers have at least citations[i] citations" holds automatically. The condition to test is therefore just citations[i] >= i + 1, and the answer is the largest i+1 for which it holds.
Once the condition fails it can never hold again — citations are descending while the index requirement rises — so the loop can break immediately.
There is an O(n) alternative worth knowing: the h-index never exceeds n, so bucket the citation counts into n+1 slots (capping at n) and walk down accumulating a running total. That is counting sort applied to the same argument.
Approach
- Sort descending.
- Walk until
citations[i] < i + 1, tracking the lasti+1that satisfied it.
Dry Run
citations = [3,0,6,1,5]
sorted desc [6,5,3,1,0]
i=0 6 >= 1 h = 1
i=1 5 >= 2 h = 2
i=2 3 >= 3 h = 3
i=3 1 >= 4? no -> break
answer 3
check: three papers (6, 5, 3) have at least 3 citations,
and no four papers have at least 4 ✓Complexity
- Time — O(n log n): the sort dominates; the scan is linear. The counting-sort variant is O(n).
- Space — O(log n) in Java and C++, O(n) in Python, all of it the sort — an in-place quicksort recurses, Python's Timsort allocates a merge buffer. The scan itself holds one counter.
Common Pitfalls
- Sorting ascending and then reasoning about
n - i, which is correct but easier to get wrong by one. If you do, the condition iscitations[i] >= n - i. - Comparing against
irather thani + 1. Indices are zero-based and the count of papers is not. - Returning the citation value instead of the count of papers.
- Claiming O(1) space while sorting.
- Continuing past the first failure. Harmless, but the break is what makes the early exit explicit.
Full Solution — all languages
class Solution:
def hIndex(self, nums):
nums.sort(reverse=True)
h = 0
for i, c in enumerate(nums):
if c >= i + 1:
h = i + 1
else:
break
return himport java.util.*;
class Solution {
public int hIndex(int[] nums) {
Arrays.sort(nums);
int n = nums.length, h = 0;
for (int i = 0; i < n; i++) {
int c = nums[n - 1 - i];
if (c >= i + 1) h = i + 1;
else break;
}
return h;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int hIndex(vector<int>& nums) {
sort(nums.begin(), nums.end());
int n = (int)nums.size(), h = 0;
for (int i = 0; i < n; i++) {
int c = nums[n - 1 - i];
if (c >= i + 1) h = i + 1;
else break;
}
return h;
}
};/**
* @param {number[]} nums
* @return {number}
*/ var hIndex = function(nums) {
nums.sort((a, b) => b - a);
let h = 0;
for (let i = 0; i < nums.length; i++) {
if (nums[i] >= i + 1) h = i + 1;
else break;
}
return h;
};