H-Index — LeetCode 274 Python Solution
- Problem
- #274
- Pattern
- Sorting
- 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, 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 = [3,0,6,1,5]
- Output
- 3
- Explanation
- [3,0,6,1,5] means the researcher has 5 papers in total and each of them had received 3, 0, 6, 1, 5 citations respectively.
Python solution
class Solution:
def hIndex(self, citations: List[int]) -> int:
citations.sort(reverse=True)
for h in range(len(citations), 0, -1):
if citations[h - 1] >= h:
return h
return 0Complexity
| Measure | Complexity |
|---|---|
| Time | O(n log n) |
| Space | O(1) to O(n) auxiliary |
Pattern: Sorting
Spend O(n log n) once to buy an ordering that makes the rest of the problem trivial. LeetCode 274. H-Index is filed here because LeetCode tags it Sorting and Counting Sort, which is the vocabulary this hub collects.
The sorting guide has the Python template for the pattern and the 401 LeetCode problems that use it.
Related problems
On a study list
This problem is on Top Interview 150.
Frequently asked questions
- How hard is LeetCode 274. H-Index?
- LeetCode 274. H-Index is rated Medium on LeetCode.
- What topics does LeetCode 274. H-Index cover?
- LeetCode 274. H-Index is tagged Array, Counting Sort and Sorting on LeetCode.