1. Exhaustive Candidate Search (Brute Force)
Intuition
By definition, the H-index cannot exceed the total number of papers n. We can test candidate values h from n down to 0. For each candidate h, we iterate through the entire citations array and count how many papers have citations >= h. The first candidate h where count >= h is the maximum possible H-index.
When to apply: Use to understand the mathematical definition of H-index before optimizing with sorting.
Candidate Threshold Scan (O(N^2) Time)
2 of 2 questions left todayA valid H-index requires at least h papers with >= h citations
A valid H-index requires at least h papers with >= h citations.
public class Main { public static class Solution { public int hIndex(int[] citations) { int n = citations.length; for (int h = n; h >= 1; h--) { int count = 0; for (int c : citations) { if (c >= h) count++; } if (count >= h) return h; } return 0; } } public static void main(String[] args) { Solution sol = new Solution(); int[] citations = {3, 0, 6, 1, 5}; System.out.println("H-Index: " + sol.hIndex(citations)); }}Time & Space Complexity
- Time complexity:
O(n^2)— Testing each candidate h from n down to 0 takes O(n) trials, and each trial scans the n papers. - Space complexity:
O(1)— Only constant extra variables (candidate h and count) are maintained.
Why Brute Force Doesn't Scale:
For N = 105, quadratic O(N2) approaches require ~1010 operations, exceeding the standard 1-second CPU budget.
The optimal approach processes N elements in a single or logarithmic pass, completing in under 10ms with minimal memory footprint.