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.
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.length
1≤n≤105
0≤citations[i]≤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)