H-Index – Solution & Complexity
Solution Walkthrough
Step 1: Restate the definition
- The h-index is the largest
hfor which at leasthpapers each have>= hcitations. - Equivalently, if you sort citations in descending order, it is the largest
hwhere theh-th most-cited paper (1-indexed) still has at leasthcitations.
- The h-index is the largest
Step 2: Sort-and-scan approach
- Sort the citations in descending order.
- Walk the sorted list; while the paper at position
i(0-indexed) has>= i + 1citations, the h-index is at leasti + 1. - The first position that fails this test ends the run, so the last satisfied
i + 1is the answer.
Step 3: Sort-based solution
- Sorting dominates the cost:
O(n log n)time,O(1)orO(n)extra space depending on the sort.
- Sorting dominates the cost:
Step 4: Bucket counts to drop the sort
- Any citation count larger than
n(the number of papers) can only ever contribute to an h-index of at mostn, so clamp it into a singlenbucket. - Build
buckets[0..n]counting how many papers have each (clamped) citation count. - Sweep from the highest bucket down, accumulating how many papers have at least
icitations; the firstiwhere that running total reachesiis the h-index.
- Any citation count larger than
Step 5: Optimal O(n) counting solution
- Counting sort over the clamped values gives linear time and
O(n)space.
- Counting sort over the clamped values gives linear time and
Step 6: Common mistakes
- Comparing against
iinstead ofi + 1when scanning a 0-indexed descending array. - Forgetting to clamp citation counts above
n, which breaks the bucket sizing. - Returning after the first failure without having recorded the last valid
hin the sort-based version.
- Comparing against