http://en.wikipedia.org/wiki/H-index
此维基页面是h-index
的定义基本上如果我有一个[0 3 4 7 8 9 10]的数组,我的h指数将是4,因为我有4个大于4的数字。如果我是的话我的h指数是5如果有一个大于或等于0的整数数组,那么有效计算h指数的方法是什么?
编辑:数组未必排序
答案 0 :(得分:12)
这里我的实现 O(N)有表格,这很简单,也很快:
private static int GetHIndex(int[] m)
{
int[] s = new int[m.Length + 1];
for (int i = 0; i < m.Length; i++) s[Math.Min(m.Length, m[i])]++;
int sum = 0;
for (int i = s.Length - 1; i >= 0; i--)
{
sum += s[i];
if (sum >= i)
return i;
}
return 0;
}
答案 1 :(得分:1)
这可以在O(n)时间内完成。
这里我假设n是奇数。偶数n改变算法(假设中位数为n / 2,用n / 2替换(n + 1)/ 2)。而且,在O(n)时间内找到实际中值是复杂的。改为使用好的支点(如快速排序)。
复杂度:n + n / 2 + n / 4 ... = O(n)
答案 2 :(得分:1)
这是O(nlogn)时间,但要简洁明了。
public static int hindex(int[] array) {
Arrays.sort(array);
int pos = 0;
while (pos < array.length && array[pos] <= array.length - pos) {
pos++;
}
return array[pos - 1];
}
答案 3 :(得分:0)
这是我能想到的一个解决方案。不确定它是否是最好的。
按升序对数组进行排序。复杂性 nlog(n)
从索引 0到n 遍历数组。 n
的复杂性并且对于每次迭代,假设索引是 i
if (arr[i] == (arr.length - (i+1))
return arr[i]
例如,
arr =[ 0 3 4 7 8 9 10 ]
arr[2] = 4
i = 2
arr.length = 7
4 = (7- (2+1))
答案 4 :(得分:0)
在C#中回答,但也可以轻松转换为Java
public int HIndex(int[] citations) {
Array.Sort(citations);
var currentCount = 0;
var length = citations.Length;
for (var i = citations.Length - 1; i >= 0; i--)
{
currentCount = length - i;
// if the count of items to the right is larger than current value it means thats the max we can expect for hindex
if (currentCount - 1 >= citations[i])
{
return currentCount - 1;
}
}
return currentCount;
}
答案 5 :(得分:0)
n =数组大小
对数组进行排序
然后h-index = max(min(f(i),i)等于i = 1:n)
由于h-index永远不能超过n,因此请将数组中的所有数字替换为更大的数字 比n加上n。
现在使用count sort对数组进行排序。
时间复杂度O(n) 空间复杂度O(n)
答案 6 :(得分:-1)
我对之前的实现感到不满意,所以我用一个用Java编写的更快的解决方案替换它。
public int hIndex(int[] citations) {
if(citations == null || citations.length == 0)
{
return 0;
}
Arrays.sort(citations);
int hIndex = 0;
for(int i=0;i<citations.length;i++)
{
int hNew;
if(citations[i]<citations.length-i)
{
hNew = citations[i];
if(hNew>hIndex)
{
hIndex = hNew;
}
}
else if(citations[i]>=citations.length-i)
{
hNew = citations.length-i;
if(hNew>hIndex)
{
hIndex = hNew;
}
break;
}
}
return hIndex;
}