243. H-Index
A researcher has published one paper per entry of the integer array citations, where citations[i] is how many times paper i has been cited. The array arrives in no particular order.
Their h-index is the largest integer h such that at least h of the papers have h or more citations each. Return that number.
For example, an h-index of 3 means the researcher can point to 3 papers with at least 3 citations apiece — but cannot point to 4 papers with at least 4.
Example 1:
Input: citations = [3, 0, 6, 1, 5]
Output: 3
Explanation: Three papers (with 6, 5, and 3 citations) each have at least 3 citations, but there are not four papers with 4 or more, so h = 3.
Example 2:
Input: citations = [1, 3, 1]
Output: 1
Explanation: All three papers have at least 1 citation, giving h >= 1, but only one paper reaches 2 citations, so h = 1 (note h can exceed 1 only if at least 2 papers have 2+).
Constraints:
- 1 ≤ citations.length ≤ 5000
- 0 ≤ citations[i] ≤ 1000
Hints:
Sort the papers from most cited to least. Walking down that list, the paper at 1-based position i can support an h of i exactly when it has at least i citations.
You can beat sorting: an h-index can never exceed n, so citation counts above n are interchangeable. Clamp each count to n, bucket-count them, and sweep the buckets from n downward until the running total of papers reaches the bucket index.
▶ Run checks these sample cases. Submit also runs hidden edge cases.
Input: citations = [3, 0, 6, 1, 5]
Expected output: 3