#275
Medium Algorithms H index ii
Array Binary Search
39.4% acceptance
Jan 12, 2026
528
162
Given an array of integers citations where citations[i] is the number of citations a researcher received for their ith paper and citations is sorted in non-descending order, return the researcher's h-index.
According to the definition of h-index on Wikipedia: The h-index is defined as the maximum value of h such that the given researcher has published at least h papers that have each been cited at least h times.
You must write an algorithm that runs in logarithmic time.
Solution
Rust
Time O(n log n)
Space O(1)
impl Solution {
pub fn h_index_ii(citations: Vec<i32>) -> i32 {
let n = citations.len();
let mut left = 0;
let mut right = n;
while left < right {
let mid = left + (right - left) / 2;
if citations[mid] >= (n - mid) as i32 {
right = mid;
} else {
left = mid + 1;
}
}
(n - left) as i32
}
}