Algorithm record
LeetCode topic:
You are given an integer array citations, where citations[i] represents the number of times the researcher's ith paper has been cited, citations has been sorted in ascending order. Calculates and returns the researcher's h-index.
Please design and implement an algorithm with logarithmic time complexity to solve this problem.
illustrate
1. The topic
输入:citations = [3,0,6,1,5]
输出:3
解释:给定数组表示研究者总共有 5 篇论文,每篇论文相应的被引用了 3, 0, 6, 1, 5 次。
由于研究者有 3 篇论文每篇至少被引用了 3 次,其余两篇论文每篇被引用不多于 3 次,所以她的 h 指数是 3。
复制代码
2. Analysis
- This question is the same as the H index , but requires a
logn
level . - And when you think of this level of query calculation, it must be a two-point play. Before, we increased it through a step-by-step traversal from the front to the back, and you can cut half of it with one two-point.
class Solution {
public int hIndex(int[] citations) {
int n = citations.length;
int left = 0, right = n - 1;
while (left <= right) {
int mid = (right + left) / 2;
if (citations[mid] >= n - mid) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return n - left;
}
}
复制代码
Summarize
Binary search.