H-Index II — LeetCode 275 Python Solution
- Problem
- #275
- Pattern
- Monotonic Stack
- Reading time
- 2 min
- Source
- leetcode.com
The problem
Given an array of integers citations where citations[i] is the number of citations a researcher received for their ith paper and citations is sorted in non-descending order, return the researcher's h-index. According to the definition of h-index on Wikipedia: The h-index is defined as the maximum value of h such that the given researcher has published at least h papers that have each been cited at least h times.
Example
- Input
- citations = [0,1,3,5,6]
- Output
- 3
- Explanation
- [0,1,3,5,6] means the researcher has 5 papers in total and each of them had received 0, 1, 3, 5, 6 citations respectively.
Python solution
class Solution:
def hIndex(self, citations: List[int]) -> int:
n = len(citations)
left, right = 0, n
while left < right:
mid = (left + right + 1) >> 1
if citations[n - mid] >= mid:
left = mid
else:
right = mid - 1
return leftComplexity
| Measure | Complexity |
|---|---|
| Time | O(\log n), where n is the length of the array citations |
| Space | O(1) auxiliary |
Pattern: Monotonic Stack
Answer "what is the next greater element" for every position in one pass. LeetCode 275. H-Index II is filed here because the reference solution below belongs to the algorithm family this hub collects, even though its LeetCode tags point elsewhere.
The monotonic stack guide has the Python template for the pattern and the 225 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 275. H-Index II?
- LeetCode 275. H-Index II is rated Medium on LeetCode.
- What is the time complexity of LeetCode 275. H-Index II?
- The Python solution on this page runs in O(\log n), where n is the length of the array citations.
- What is the space complexity of LeetCode 275. H-Index II?
- The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 275. H-Index II cover?
- LeetCode 275. H-Index II is tagged Array and Binary Search on LeetCode.