InterviewPrepKit

Home / Coding / Arrays & Hashing

H-Index

medium Original ↗
Solving tips
  • h can never exceed the number of papers n, so clamp every citation to min(c, n) and the value domain shrinks to 0..n.
  • That bound enables counting-sort: bucket papers by clamped count, sweep h from n down accumulating papers with >= h citations, return the first h where the running total reaches h (O(n) time, O(n) space).
  • Sorting alternative: sort descending and find the last index i where citations[i] >= i + 1 (O(n log n)); mind the 0-based off-by-one.
  • Watch the all-highly-cited case ([100] -> 1, not 100).

Problem

You are given an array citations where citations[i] is the number of citations a researcher’s i-th paper received. Compute the researcher’s h-index: the largest number h such that the researcher has at least h papers with at least h citations each.

Put differently: find the biggest h for which the claim “h of my papers each got h or more citations” is true. h can never exceed the number of papers.

Examples

Example 1: citations = [3,0,6,1,5]3 Three papers (3, 6, 5 citations) each have ≥ 3 citations; four papers with ≥ 4 each doesn’t hold (only 6 and 5 qualify).

Example 2: citations = [1,3,1]1 Only one paper has ≥ 2 citations, so h = 2 fails; h = 1 holds (three papers have ≥ 1… one suffices).

Example 3: citations = [100]1 One paper, however famous, caps h at 1.

Constraints

  • n == citations.length, 1 <= n <= 5000
  • 0 <= citations[i] <= 1000

n ≤ 5000 lets an O(n²) scan pass, but the intended answers are O(n log n) via sorting or O(n) via counting — and the O(n) one exploits that h can never exceed n.

Think about it first

Hint 1 For a fixed candidate value h, how do you check in one pass whether the h-index is at least h?
Hint 2 Sort the citations in descending order. Reading left to right, position i (0-based) tells you "there are i+1 papers with at least citations[i] citations". When does the h condition hold?
Hint 3 Any citation count above n is no more useful than exactly n. Bucket-count papers by min(citations, n), then sweep h from n down to 0, accumulating how many papers have ≥ h citations; the first h where that running total reaches h is the answer.
Your workspace Not runnable by design — this is your interview scratchpad. Saved on this device.