#274

H-Index

Medium Counting sort

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

citations[i] is how many citations paper i has. The h-index is the largest h such that at least h papers have h or more citations each. Return it.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: the biggest h where at least h papers have h or more citations.

Why this works: h can't exceed n, so bucket citations (capping at n) and walk from high to low, adding up papers.

Example: [3, 0, 6, 1, 5], n = 5

buckets: 0→1, 1→1, 3→1, 5→2 (6 capped to 5)h=5: 2 papers, h=4: 2, h=3: 3 ≥ 3 → answer 3

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Off-by-one when sorting: after sorting ascending, the check is citations[i] >= n - i, not > .