CodingNodes / Arrays & Hashing / H-Index System design Space play · ←→ step · R restart

Problem

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

The h-index is the largest number h such that at least h of the papers have h or more citations each. Return it.

Constraints

  • 1 ≤ citations.length ≤ 5000
  • 0 ≤ citations[i] ≤ 1000

Examples: pick one to animate

Solutions

TimeO(n log n) SpaceO(n)

Time: Sorting costs n log n and the scan after it is one pass.

Space: We sort a copy of the array so the input stays unchanged.

Sort the counts from smallest to largest. At index i, there are n minus i papers from i to the end, and all of them have at least ordered[i] citations. The first index where ordered[i] is at least n minus i gives the biggest h.