H-Index II

Given an array `citations` sorted in non-decreasing order, where `citations[i]` is the number of citations of the researcher's `i`-th paper, return the h-index: the largest `h` such that at least `h` papers have at least `h` citations each. The algorithm must run in logarithmic time.

30:00

H-Index II
medium
Free written solution
Topics
Companies

Given an array citations sorted in non-decreasing order, where citations[i] is the number of citations of the researcher's i-th paper, return the h-index: the largest h such that at least h papers have at least h citations each. The algorithm must run in logarithmic time.

Example 1:
Input: {"citations":[0,1,3,5,6]}
Output: 3
Constraints:
  • n==citations.lengthn == \text{citations.length}

  • 1≤n≤1051 \leq n \leq 10^5

  • 0≤citations[i]≤10000 \leq \text{citations}[i] \leq 1000

  • citations is sorted in ascending order.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"citations":[0,1,3,5,6]}

The papers from index i to the end are the n - i most cited ones. Index i proves h = n - i when citations[i] >= n - i. Binary search for the first such i in [0, 4].

n 5

lo 0

hi 4

mid -

n - mid -

citations (bars) vs. papers from i to the end (dashes)

0

1

3

5

6

0

lo

1

2

3

4

hi

Variables
VariableValue
n5
lo0
hi4
midnull
DepthFunction Call
Stack empty
0/7