H-Index II
Medium
Topics
Given an array nums of citation counts sorted in ascending order, return the researcher's h-index: the maximum value h such that at least h papers have at least h citations each. Solve in O(log n).
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: nums = [0,1,3,5,6] Output: 3
Example 2
Input: nums = [1,2,100] Output: 2
Example 3
Input: nums = [0] Output: 0
Constraints
- 1 <= nums.length <= 10^5
- 0 <= nums[i] <= 1000
- nums is sorted in ascending order.
Editorial — intuition, approach, complexity and pitfalls
Intuition
Same definition as the h-index, but the array arrives already sorted ascending and the problem asks for logarithmic time. Sorting again would waste the gift; the ascending order is what makes a binary search possible.
With n papers sorted ascending, the papers from index i onward number n - i, and all of them have at least citations[i] citations. So index i supports an h-index of n - i exactly when citations[i] >= n - i.
That predicate is monotonic: as i rises, citations[i] only grows while n - i only shrinks, so once it becomes true it stays true. Binary search for the first index where it holds; the answer is n minus that index.
Approach
- Binary search over indices for the first
iwithcitations[i] >= n - i. - Return
n - lo.
Dry Run
citations = [0,1,3,5,6] n = 5
lo=0 hi=4 mid=2 citations[2]=3 >= 5-2=3 true -> hi=1
lo=0 hi=1 mid=0 citations[0]=0 >= 5-0=5 false -> lo=1
lo=1 hi=1 mid=1 citations[1]=1 >= 5-1=4 false -> lo=2
lo > hi, lo = 2
answer n - lo = 3
check: papers at indices 2,3,4 have 3,5,6 citations --
three papers with at least 3 each ✓Complexity
- Time — O(log n): one binary search. The O(n) scan over a sorted array would also pass but ignores the stated requirement.
- Space — O(1): and genuinely so, unlike the unsorted variant where the sort costs memory. That is the real difference between the two problems.
Common Pitfalls
- Getting the predicate backwards. For ascending order it is
citations[i] >= n - i; the descending variant usesi + 1. - Returning
lorather thann - lo. The answer is a count of papers, not an index. - Searching for the last false index instead of the first true one, which lands one off.
- Sorting the input, which is already sorted and would make the solution O(n log n) for no reason.
- Scanning linearly. Correct, but the problem names the logarithmic bound.
Full Solution — all languages
class Solution:
def hIndex(self, nums):
n = len(nums)
lo, hi = 0, n - 1
while lo <= hi:
mid = (lo + hi) // 2
if nums[mid] >= n - mid:
hi = mid - 1
else:
lo = mid + 1
return n - loimport java.util.*;
class Solution {
public int hIndex(int[] nums) {
int n = nums.length;
int lo = 0, hi = n - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (nums[mid] >= n - mid) hi = mid - 1;
else lo = mid + 1;
}
return n - lo;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int hIndex(vector<int>& nums) {
int n = (int)nums.size();
int lo = 0, hi = n - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (nums[mid] >= n - mid) hi = mid - 1;
else lo = mid + 1;
}
return n - lo;
}
};/**
* @param {number[]} nums
* @return {number}
*/ var hIndex = function(nums) {
const n = nums.length;
let lo = 0, hi = n - 1;
while (lo <= hi) {
const mid = (lo + hi) >> 1;
if (nums[mid] >= n - mid) hi = mid - 1;
else lo = mid + 1;
}
return n - lo;
};