给定一个数组arr[]由 N 个以非递增顺序表示的整数组成,表示引用,任务是找到 H-index。
H-Index is usually assigned to the researcher denoting the contributions made in terms of no of papers and citations. H-index(H) is the largest value such that the researcher has at least H papers cited at least H times.
例子:
Input: arr[] = {5, 3, 3, 0, 0}
Output: 3
Explanation:
There are atleast 3 papers (5, 3, 3) with atleast 3 citations
Input: arr[] = {5, 4, 2, 1, 1}
Output: 2
Explanation:
There are atleast 2 papers (5, 4, 2) with atleast 2 citations.
朴素的方法:一个简单的解决方案是从左到右遍历论文,并在引用i大于或等于索引时增加 H-index。
时间复杂度: O(N)
Efficient Approach:想法是使用二分搜索来优化上述方法。 H-index 可以在0到N的范围内。要检查给定值是否可能,请检查citations[value]是否大于或等于value 。
- 将二元搜索的搜索范围初始化为0 到 N 。
- 找到范围的中间元素。
- 检查引用的中间元素是否小于索引。如果是,则将左侧范围更新为中间元素。
- 否则,检查引用的中间元素是否大于索引。如果是,则将正确的范围更新到中间元素。
- 否则,给定的索引是引文的 H 索引。
下面是上述方法的实现:
C++
// C++ implementation of the
// above approach
#include
using namespace std;
// Function to find the H-index
int hIndex(vector citations,
int n)
{
int hindex = 0;
// Set the range for binary search
int low = 0, high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
// Check if current citations is
// possible
if (citations[mid] >= (mid + 1)) {
// Check to the right of mid
low = mid + 1;
// Update h-index
hindex = mid + 1;
}
else {
// Since current value is not
// possible, check to the left
// of mid
high = mid - 1;
}
}
// Print the h-index
cout << hindex << endl;
return hindex;
}
// Driver Code
int main()
{
// citations
int n = 5;
vector citations = { 5, 3, 3, 2, 2 };
hIndex(citations, n);
}
Java
// Java implementation of the
// above approach
import java.io.*;
class GFG{
// Function to find the H-index
static int hIndex(int[] citations, int n)
{
int hindex = 0;
// Set the range for binary search
int low = 0, high = n - 1;
while (low <= high)
{
int mid = (low + high) / 2;
// Check if current citations is
// possible
if (citations[mid] >= (mid + 1))
{
// Check to the right of mid
low = mid + 1;
// Update h-index
hindex = mid + 1;
}
else
{
// Since current value is not
// possible, check to the left
// of mid
high = mid - 1;
}
}
// Print the h-index
System.out.println(hindex);
return hindex;
}
// Driver Code
public static void main (String[] args)
{
// citations
int n = 5;
int[] citations = { 5, 3, 3, 2, 2 };
hIndex(citations, n);
}
}
// This code is contributed by sanjoy_62
Python3
# Python3 implementation of the
# above approach
# Function to find the H-index
def hIndex(citations, n):
hindex = 0
# Set the range for binary search
low = 0
high = n - 1
while (low <= high):
mid = (low + high) // 2
# Check if current citations is
# possible
if (citations[mid] >= (mid + 1)):
# Check to the right of mid
low = mid + 1
# Update h-index
hindex = mid + 1
else:
# Since current value is not
# possible, check to the left
# of mid
high = mid - 1
# Print the h-index
print(hindex)
return hindex
# Driver Code
# citations
n = 5
citations = [ 5, 3, 3, 2, 2 ]
# Function Call
hIndex(citations, n)
# This code is contributed by Shivam Singh
C#
// C# implementation of the
// above approach
using System;
class GFG{
// Function to find the H-index
static int hIndex(int[] citations, int n)
{
int hindex = 0;
// Set the range for binary search
int low = 0, high = n - 1;
while (low <= high)
{
int mid = (low + high) / 2;
// Check if current citations is
// possible
if (citations[mid] >= (mid + 1))
{
// Check to the right of mid
low = mid + 1;
// Update h-index
hindex = mid + 1;
}
else
{
// Since current value is not
// possible, check to the left
// of mid
high = mid - 1;
}
}
// Print the h-index
Console.WriteLine(hindex);
return hindex;
}
// Driver Code
public static void Main ()
{
// citations
int n = 5;
int[] citations = { 5, 3, 3, 2, 2 };
hIndex(citations, n);
}
}
// This code is contributed by sanjoy_62
Javascript
3
时间复杂度: O(logN)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live