1. Reverse Linear Scan (Brute Force)
Intuition
Since citations is already sorted ascendingly, at any index i there are exactly n - i papers with at least citations[i] citations. We scan from i = 0 to n - 1. The first index i where citations[i] >= n - i gives the H-index n - i.
When to apply: Use to understand the sorted property relationship before utilizing binary search.
All Suffixes Verification (O(N) Time)
2 of 2 questions left todayPaper 4 has 6 citations, which supports h = 1
Paper 4 has 6 citations, which supports h = 1.
public class Main { public static class Solution { public int hIndex(int[] citations) { int n = citations.length; for (int i = 0; i < n; i++) { if (citations[i] >= n - i) { return n - i; } } return 0; } } public static void main(String[] args) { Solution sol = new Solution(); int[] citations = {0, 1, 3, 5, 6}; System.out.println("H-Index: " + sol.hIndex(citations)); }}Time & Space Complexity
- Time complexity:
O(n)— A single linear scan through the n elements takes O(n) time. - Space complexity:
O(1)— No extra memory is allocated.
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.