Skip to content
Problem 274 · LeetCode 274(opens in a new tab)

H-Index

MediumArrays

A researcher has written several papers. You get citations, where citations[i] is how many times paper i has been cited by other work.

Their h-index is the largest number h such that h of their papers have been cited h times or more each. Return it. A researcher whose papers were never cited has an h-index of 0.

Example 1

Input:  citations = [4, 0, 7, 2, 5]
Output: 3
Why:    7, 5 and 4 are all ≥ 3, but only those 3 papers are ≥ 4
2 more examples and the constraints

Example 2

Input:  citations = [2, 2, 1]
Output: 2
Why:    two papers have at least 2 citations each

Example 3

Input:  citations = [0, 0]
Output: 0
Why:    no paper was cited at all

Constraints

  • 1 ≤ length of citations ≤ 5,000
  • 0 ≤ citations[i] ≤ 1,000

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Sort, most cited firstO(n log n)
  2. Counting bucketsO(n)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for 5,000 papersVerdict
Sort, most cited firstO(n log n)
Passes; the easy one to explain
Counting bucketsO(n)
Best; no sort needed

n = number of papers.

Edge cases to test

  • Nothing cited. [0, 0] has an h-index of 0.
  • One paper. [100] gives 1: one paper can never give more than 1.
  • Every paper clears n. [5, 5, 5, 5, 5] gives 5, the most possible.
  • Ties. [2, 2, 1] gives 2: equal values both count.
  • Huge citation totals. Values above n must land in bucket n, not off the end of the array.

Common mistakes

  • Returning the citation value where the walk stops instead of the count h.
  • Using ≥ instead of > in the sorted walk, which is off by one: the paper at position h is the (h + 1)-th paper.
  • Making a bucket for every citation value up to 1,000 and forgetting to cap at n.

How to talk through it

  1. 1Clarify. Can the answer be 0? Can a citation total be larger than the number of papers?
  2. 2Pin the definition. h papers with at least h citations each. Say that h is at most n.
  3. 3Give the sort. Sort descending and grow h while the next paper clears h. O(n log n).
  4. 4Drop the sort. Cap values at n and count them into n + 1 buckets, then walk h down from n.
  5. 5Test out loud. Run [0, 0], [100] and a case with ties.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?