LeetCode 题解工作台

H 指数

给你一个整数数组 citations ,其中 citations[i] 表示研究者的第 i 篇论文被引用的次数。计算并返回该研究者的 h 指数 。 根据维基百科上 h 指数的定义 : h 代表“高引用次数” ,一名科研人员的 h 指数 是指他(她)至少发表了 h 篇论文,并且 至少 有 h 篇论文被…

category

3

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·排序

bolt

答案摘要

我们可以先对数组 `citations` 按照元素值从大到小进行排序。然后我们从大到小枚举 值,如果某个 值满足 $citations[h-1] \geq h$,则说明有至少 篇论文分别被引用了至少 次,直接返回 即可。如果没有找到这样的 值,说明所有的论文都没有被引用,返回 。 时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 是数组 …

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·排序 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个整数数组 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.length
  • 1 <= n <= 5000
  • 0 <= citations[i] <= 1000
lightbulb

解题思路

方法一:排序

我们可以先对数组 citations 按照元素值从大到小进行排序。然后我们从大到小枚举 hh 值,如果某个 hh 值满足 citations[h1]hcitations[h-1] \geq h,则说明有至少 hh 篇论文分别被引用了至少 hh 次,直接返回 hh 即可。如果没有找到这样的 hh 值,说明所有的论文都没有被引用,返回 00

时间复杂度 O(n×logn)O(n \times \log n),空间复杂度 O(logn)O(\log n)。其中 nn 是数组 citations 的长度。

1
2
3
4
5
6
7
8
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
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • 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.

warning

常见陷阱

外企场景
  • 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.

swap_horiz

进阶变体

外企场景
  • 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.

help

常见问题

外企场景

H 指数题解:数组·排序 | LeetCode #274 中等