英文原文
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 an ascending order, return compute the researcher's h-index.
According to the definition of h-index on Wikipedia: A scientist has an index h if h of their n papers have at least h citations each, and the other n − h papers have no more than h citations each.
If there are several possible values for h, the maximum one is taken as the h-index.
You must write an algorithm that runs in logarithmic time.
Example 1:
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. Since the researcher has 3 papers with at least 3 citations each and the remaining two with no more than 3 citations each, their h-index is 3.
Example 2:
Input: citations = [1,2,100] Output: 2
Constraints:
n == citations.length1 <= n <= 1050 <= citations[i] <= 1000citationsis sorted in ascending order.
中文题目
给你一个整数数组 citations ,其中 citations[i] 表示研究者的第 i 篇论文被引用的次数,citations 已经按照 升序排列 。计算并返回该研究者的 h 指数。
h 指数的定义:h 代表“高引用次数”(high citations),一名科研人员的 h 指数是指他(她)的 (n 篇论文中)总共有 h 篇论文分别被引用了至少 h 次。且其余的 n - h 篇论文每篇被引用次数 不超过 h 次。
提示:如果 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按 升序排列
通过代码
高赞题解
基本分析
本题与 274. H 指数 的主要不同有两方面:
- 数据范围不同:切换成英文可以发现,在 274. H 指数 里 $n$ 的范围为 $5000$,而本题 $n$ 的范围为 $10^5$ ;
- 给定数组是否有序:在 274. H 指数 中数组不一定有序,本题则是有序。
显然,增加了数组有序特性,扩大了数据范围。可以猜到利用此特性,存在时间复杂度更低的算法实现。
二分答案(线性 check)
在 (题解) 274. H 指数 中,我们使用了 $O(n\log{n})$ 的二分做法,算法的主要瓶颈在于 $O(n)$ 复杂度的 check。
当然对于 $10^5$ 的数据量,使用 $O(n\log{n})$ 复杂度没有任何问题。
代码:
[]class Solution { public int hIndex(int[] cs) { int n = cs.length; int l = 0, r = n; while (l < r) { int mid = l + r + 1 >> 1; if (check(cs, mid)) l = mid; else r = mid - 1; } return r; } boolean check(int[] cs, int mid) { int ans = 0; for (int i : cs) if (i >= mid) ans++; return ans >= mid; } }
- 时间复杂度:对 $[0, n]$ 做二分,复杂度为 $O(\log{n})$;
check函数需要对数组进行线性遍历,复杂度为 $O(n)$。整体复杂度为 $O(n\log{n})$ - 空间复杂度:$O(1)$
二分下标(根据与 $citations[i]$ 关系)
在解法一中,显然我们没有利用本题的「数组有序」的特性。
根据对 H 指数 定义,如果 $citations$ 升序,在最大的符合条件的分割点 $x$ 的右边(包含分割点),必然满足 $citations[i] >= x$,我们应当对其进行计数,对于分割点的左边,必然不满足 $citations[i] >= x$,无需进行计数。
因此,我们可以利用 分割点右边论文的个数与分割点 $citations[x]$ 的大小关系进行二分 。
假设存在真实分割点下标 $x$,其值大小为 $citations[x]$,分割点右边的数值个数为 $n - x$,根据 H 指数 的定义,必然有 $citations[x] >= n - x$ 关系:
- 在分割点 $x$ 的右边:$citations[i]$ 非严格单调递增,而论文的个数严格单调递减,仍然满足 $citations[i] >= n - i$ 关系;
- 在分割点 $x$ 的左边:$citations[i]$ 非严格单调递减,论文的个数严格单调递增,$x$ 作为真实分割点,因此必然不满足 $citations[i] >= n - i$ 关系。
利用此「二段性」进行二分即可,二分出下标后,再计算出论文的个数。
代码:
[]class Solution { public int hIndex(int[] cs) { int n = cs.length; int l = 0, r = n - 1; while (l < r) { int mid = l + r >> 1; if (cs[mid] >= n - mid) r = mid; else l = mid + 1; } return cs[r] >= n - r ? n - r : 0; } }
- 时间复杂度:$O(\log{n})$
- 空间复杂度:$O(1)$
其他「二分」相关内容
| 题目 | 题解 | 难度 | 推荐指数 |
|---|---|---|---|
| 4. 寻找两个正序数组的中位数 | LeetCode 题解链接 | 困难 | 🤩🤩🤩🤩 |
| 29. 两数相除 | LeetCode 题解链接 | 中等 | 🤩🤩🤩 |
| 33. 搜索旋转排序数组 | LeetCode 题解链接 | 中等 | 🤩🤩🤩🤩🤩 |
| 34. 在排序数组中查找元素的第一个和最后一个位置 | LeetCode 题解链接 | 中等 | 🤩🤩🤩🤩🤩 |
| 35. 搜索插入位置 | LeetCode 题解链接 | 简单 | 🤩🤩🤩🤩🤩 |
| 74. 搜索二维矩阵 | LeetCode 题解链接 | 中等 | 🤩🤩🤩🤩 |
| 81. 搜索旋转排序数组 II | LeetCode 题解链接 | 中等 | 🤩🤩🤩🤩 |
| 153. 寻找旋转排序数组中的最小值 | LeetCode 题解链接 | 中等 | 🤩🤩🤩 |
| 154. 寻找旋转排序数组中的最小值 II | LeetCode 题解链接 | 困难 | 🤩🤩🤩 |
| 220. 存在重复元素 III | LeetCode 题解链接 | 中等 | 🤩🤩🤩 |
| 274. H 指数 | LeetCode 题解链接 | 中等 | 🤩🤩🤩 |
| 278. 第一个错误的版本 | LeetCode 题解链接 | 简单 | 🤩🤩🤩🤩 |
| 354. 俄罗斯套娃信封问题 | LeetCode 题解链接 | 困难 | 🤩🤩🤩 |
| 363. 矩形区域不超过 K 的最大数值和 | LeetCode 题解链接 | 困难 | 🤩🤩🤩 |
| 374. 猜数字大小 | LeetCode 题解链接 | 简单 | 🤩🤩🤩 |
| 778. 水位上升的泳池中游泳 | LeetCode 题解链接 | 困难 | 🤩🤩🤩 |
| 852. 山脉数组的峰顶索引 | LeetCode 题解链接 | 简单 | 🤩🤩🤩🤩🤩 |
| 981. 基于时间的键值存储 | LeetCode 题解链接 | 中等 | 🤩🤩🤩🤩 |
| 1004. 最大连续1的个数 III | LeetCode 题解链接 | 中等 | 🤩🤩🤩 |
| 1011. 在 D 天内送达包裹的能力 | LeetCode 题解链接 | 中等 | 🤩🤩🤩🤩 |
| 1208. 尽可能使字符串相等 | LeetCode 题解链接 | 中等 | 🤩🤩🤩 |
| 1438. 绝对差不超过限制的最长连续子数组 | LeetCode 题解链接 | 中等 | 🤩🤩🤩 |
| 1482. 制作 m 束花所需的最少天数 | LeetCode 题解链接 | 中等 | 🤩🤩🤩 |
| 1707. 与数组中元素的最大异或值 | LeetCode 题解链接 | 困难 | 🤩🤩🤩 |
| 1751. 最多可以参加的会议数目 II | LeetCode 题解链接 | 困难 | 🤩🤩🤩 |
统计信息
| 通过次数 | 提交次数 | AC比率 |
|---|---|---|
| 52431 | 113718 | 46.1% |
提交历史
| 提交时间 | 提交结果 | 执行时间 | 内存消耗 | 语言 |
|---|
相似题目
| 题目 | 难度 |
|---|---|
| H 指数 | 中等 |