LeetCode 题解工作台
H 指数 II
给你一个整数数组 citations ,其中 citations[i] 表示研究者的第 i 篇论文被引用的次数, citations 已经按照 非降序排列 。计算并返回该研究者的 h 指数。 h 指数的定义 :h 代表“高引用次数”(high citations),一名科研人员的 h 指数是指他(她…
2
题型
7
代码语言
3
相关题
当前训练重点
中等 · 二分·搜索·答案·空间
答案摘要
我们注意到,如果有至少 篇论文的引用次数大于等于 ,那么对于任意 $y \lt x$,其引用次数也一定大于等于 。这存在着单调性。 因此,我们二分枚举 ,获取满足条件的最大 。由于要满足 篇论文至少被引用 次,因此 $citations[n - mid] \ge mid$。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 二分·搜索·答案·空间 题型思路
题目描述
给你一个整数数组 citations ,其中 citations[i] 表示研究者的第 i 篇论文被引用的次数,citations 已经按照 非降序排列 。计算并返回该研究者的 h 指数。
h 指数的定义:h 代表“高引用次数”(high citations),一名科研人员的 h 指数是指他(她)的 (n 篇论文中)至少 有 h 篇论文分别被引用了至少 h 次。
请你设计并实现对数时间复杂度的算法解决此问题。
示例 1:
输入:citations = [0,1,3,5,6]输出:3解释:给定数组表示研究者总共有5篇论文,每篇论文相应的被引用了0, 1, 3, 5, 6次。 由于研究者有3篇论文每篇 至少 被引用了3次,其余两篇论文每篇被引用 不多于3次,所以她的 h 指数是3。
示例 2:
输入:citations = [1,2,100]输出:2
提示:
n == citations.length1 <= n <= 1050 <= citations[i] <= 1000citations按 升序排列
解题思路
方法一:二分查找
我们注意到,如果有至少 篇论文的引用次数大于等于 ,那么对于任意 ,其引用次数也一定大于等于 。这存在着单调性。
因此,我们二分枚举 ,获取满足条件的最大 。由于要满足 篇论文至少被引用 次,因此 。
时间复杂度 ,其中 是数组 的长度。空间复杂度 。
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 left
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | complexity is O(log n) due to binary search over possible H-Index values. Space complexity is O(1) as only a few variables are maintained during search. |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Expecting logarithmic time using the sorted property of citations.
- question_mark
Looking for correct identification of the H-Index without linear scanning.
- question_mark
Check for off-by-one errors in determining n - index >= h condition.
常见陷阱
外企场景- error
Confusing index positions with citation counts during binary search.
- error
Failing to handle cases where citations are all lower than potential h.
- error
Using linear scan, which violates the logarithmic time requirement.
进阶变体
外企场景- arrow_right_alt
H-Index calculation where the citations array is unsorted, requiring sorting first.
- arrow_right_alt
Dynamic H-Index tracking as new citations are added to the array.
- arrow_right_alt
Compute H-Index with additional constraints, e.g., only counting papers from certain journals.