#274 H-Index
the problem
In plain words
citations[i] is the citation count of paper i. Return the largest h such that the researcher has at least h papers with at least h citations each.
in citations = [3,0,6,1,5]
out 3
Papers with 3, 6, 5 citations: three papers with ≥ 3.
in citations = [1,3,1]
out 1
- 1 ≤ n ≤ 5000
- 0 ≤ citations[i] ≤ 1000
your turn
Try it before you peek
Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.
Solve it in the browser
LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.
Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.
Marking the problem solved, or opening every hint, also shows it.
the aha
Key insight
patternCounting / bucket sort
h can never exceed n, so citation counts above n all behave the same — put them in one bucket and counting sort becomes O(n).
solutions
From brute force to optimal
For h = n down to 1, count papers with ≥ h citations.
- For each h from n down, count c ≥ h.
- Return the first h whose count ≥ h.
Up to n candidates × an O(n) count.
A counter.
class Solution { public int hIndex(int[] citations) { int n = citations.length; for (int h = n; h > 0; h--) { // try the biggest h first int atLeastH = 0; for (int c : citations) if (c >= h) atLeastH++; if (atLeastH >= h) return h; } return 0; }}In sorted order, papers i..n−1 all have ≥ c[i] citations; the first i with c[i] ≥ n − i gives h = n − i.
- Sort.
- For i from 0: if c[i] ≥ n − i, return n − i.
- Return 0.
Sorting dominates.
We sort a copy (O(log n) if sorting in place).
class Solution { public int hIndex(int[] citations) { int[] c = citations.clone(); Arrays.sort(c); // ascending int n = c.length; for (int i = 0; i < n; i++) { if (c[i] >= n - i) return n - i; // papers i..n-1 (n-i of them) all have ≥ n-i citations } return 0; }}buckets[c] = papers with exactly c citations (c capped at n). Walk h from n down, accumulating.
- buckets[min(c, n)]++.
- papers = 0; for h = n..0: papers += buckets[h]; if papers ≥ h return h.
Two linear passes over n + 1 buckets.
The bucket array.
class Solution { public int hIndex(int[] citations) { int n = citations.length; int[] buckets = new int[n + 1]; // buckets[n] collects every paper with ≥ n citations for (int c : citations) buckets[Math.min(c, n)]++; int papers = 0; // papers with ≥ h citations for (int h = n; h >= 0; h--) { papers += buckets[h]; if (papers >= h) return h; } return 0; }}in the room
How to say it in the interview
talk track
- Explain the definition with a tiny example first — the definition is the hard part.
- Then say: "h ≤ n, so I can cap counts at n and bucket them."
traps
- Returning c[i] instead of n − i in the sorted scan.
what they ask next
Follow-ups
H-Index II (LC 275): citations already sorted?
Binary search for the first i with c[i] ≥ n − i — O(log n).
before you submit
Edge cases to test
- Nobody cited anything ([0, 0, 0]) → 0.
- One paper with 100 citations → 1: h can't exceed the number of papers.
- Every paper cited more than n times → n.
practise next
Similar problems
- #347Top K Frequent ElementsMediumin Heaps
- #1636Sort Array by Increasing FrequencyEasyin Sorting