LeetCode 题解工作台
H 指数
给你一个整数数组 citations ,其中 citations[i] 表示研究者的第 i 篇论文被引用的次数。计算并返回该研究者的 h 指数 。 根据维基百科上 h 指数的定义 : h 代表“高引用次数” ,一名科研人员的 h 指数 是指他(她)至少发表了 h 篇论文,并且 至少 有 h 篇论文被…
3
题型
6
代码语言
3
相关题
当前训练重点
中等 · 数组·排序
答案摘要
我们可以先对数组 `citations` 按照元素值从大到小进行排序。然后我们从大到小枚举 值,如果某个 值满足 $citations[h-1] \geq h$,则说明有至少 篇论文分别被引用了至少 次,直接返回 即可。如果没有找到这样的 值,说明所有的论文都没有被引用,返回 。 时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 是数组 …
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 数组·排序 题型思路
题目描述
给你一个整数数组 citations ,其中 citations[i] 表示研究者的第 i 篇论文被引用的次数。计算并返回该研究者的 h 指数。
根据维基百科上 h 指数的定义:h 代表“高引用次数” ,一名科研人员的 h 指数 是指他(她)至少发表了 h 篇论文,并且 至少 有 h 篇论文被引用次数大于等于 h 。如果 h 有多种可能的值,h 指数 是其中最大的那个。
示例 1:
输入:citations = [3,0,6,1,5]输出:3 解释:给定数组表示研究者总共有5篇论文,每篇论文相应的被引用了3, 0, 6, 1, 5次。 由于研究者有3篇论文每篇 至少 被引用了3次,其余两篇论文每篇被引用 不多于3次,所以她的 h 指数是3。
示例 2:
输入:citations = [1,3,1] 输出:1
提示:
n == citations.length1 <= n <= 50000 <= citations[i] <= 1000
解题思路
方法一:排序
我们可以先对数组 citations 按照元素值从大到小进行排序。然后我们从大到小枚举 值,如果某个 值满足 ,则说明有至少 篇论文分别被引用了至少 次,直接返回 即可。如果没有找到这样的 值,说明所有的论文都没有被引用,返回 。
时间复杂度 ,空间复杂度 。其中 是数组 citations 的长度。
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 0
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Expect an initial sort-based solution and probe understanding of h-index properties.
- question_mark
Clarify whether counting sort optimization is feasible given citation constraints.
- question_mark
Check if candidate can handle edge cases like all zeros or all identical citation counts.
常见陷阱
外企场景- error
Miscounting papers with exactly h citations or assuming strict greater-than.
- error
Failing to consider empty or single-element arrays.
- error
Ignoring the trade-off between sorting and linear counting when max citation is small.
进阶变体
外企场景- arrow_right_alt
Compute h-index when citations are provided in descending order without sorting.
- arrow_right_alt
Handle streaming citation updates and dynamically maintain the h-index.
- arrow_right_alt
Calculate h-index for multiple researchers simultaneously using counting arrays.