题目描述:
Given an array of citations sorted in ascending order (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.
According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each."
Example:
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, her h-index is 3.
Note:
If there are several possible values for h, the maximum one is taken as the h-index.
Follow up:
- This is a follow up problem to H-Index, where
citationsis now guaranteed to be sorted in ascending order. - Could you solve it in logarithmic time complexity?
和H-index类似,但是引用量已经排好序,需要在O(log n)的时间复杂度内解决问题。由于是有序数组,而且给定了O(log n)的时间复杂度,所以考虑采用二分法。对于任意一个下标i,我们可以确定它对应的引用量citation[i],引用量大于等于citation[i]的论文数为n-i,那么我们利用二分法搜索下标i,使得它满足citation[i]>=(n-i)。
class Solution {
public:
int hIndex(vector<int>& citations) {
int n=citations.size();
int begin=0;
int end=n-1;
while(begin<=end)
{
int mid=(begin+end)/2;
if(citations[mid]==(n-mid)) return n-mid;
else if(citations[mid]<(n-mid)) begin=mid+1;
else if(citations[mid]>(n-mid)) end=mid-1;
}
return n-begin;
}
};
本文介绍了一种在有序数组中快速计算H-指数的方法,通过使用二分查找技巧实现O(logn)的时间复杂度。
1万+

被折叠的 条评论
为什么被折叠?



