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.length 1 <= n <= 105 0 <= citations[i] <= 1000 citations is sorted in ascending order.
题意:给定一位研究者论文被引用次数的数组(被引用次数是非负整数),数组已经按照?升序排列?。编写一个方法,计算出研究者的 h 指数。
h 指数的定义: “h 代表“高引用次数”(high citations),一名科研人员的 h 指数是指他(她)的 (N 篇论文中)总共有 h 篇论文分别被引用了至少 h 次。(其余的?N - h?篇论文每篇被引用次数不多于 h 次。)"
解法 二分
这是274. H 指数的延伸题目。由于数组已经有序,所以对于给定的 i ,我们已知有 citations.length - i 篇论文的引用次数都大于等于 citations[i] ,i 篇论文的引用数小于等于 citations[i] 。
不妨设 h = citations.length - i ,即有 h 篇论文分别引用了至少 citations[i] 次,其他 citations.length - h 篇论文的引用次数不超过 citations[i] 次。只要 citations[i] >= h ,就满足题意,有 h 篇论文的引用次数至少为 h 。因此,我们可以用二分寻找第一个满足 citations[i] >= citations.length - i 的位置 i ,答案就是 citations.length - i 。
class Solution {
public:
int hIndex(vector<int>& citations) {
int n = citations.size(), l = 0, h = n;
while (l < h) {
int m = l + (h - l) / 2;
if (citations[m] >= n - m) h = m;
else l = m + 1;
}
return n - l;
}
};
|