274.H指数Java
题目描述
给定论文被引用次数的数组(被引用次数非负),计算h 指数。
h 指数的定义:总共有 h 篇论文分别被引用了至少 h 次。且其余的 N - h 篇论文每篇被引用次数 不超过 h 次。
输入输出样式
示例1:
输入:citations = [3,0,6,1,5]
输出:3
解释:给定数组表示研究者总共有 5 篇论文,每篇论文相应的被引用了 3, 0, 6, 1, 5 次。
由于研究者有 3 篇论文每篇 至少 被引用了 3 次,其余两篇论文每篇被引用 不多于 3 次,所以她的 h 指数是 3。
本题来自LeetCode:https://leetcode-cn.com/problems/h-index/
思路
先将数组排序,然后使用二分查找。在查找中满足 citation[mid] ≥ n−mid,则移动右边界 high,否则移动左边界 low.
算法分析
时间复杂度O(n * log n),空间复杂度为O(log n)
求解函数
public int hIndex(int[] citations) {
Arrays.sort(citations);
int low = 0, high = citations.length - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (citations[mid] >= citations.length - mid) {
high = mid - 1;
} else {
low = mid + 1;
}
}
//H指数其实就是右边的元素个数
return citations.length - low;
}