H-index I
Given an array of citations (each citation was a non-negative integer) of a researcher, write a function to compute the RES Earcher ' s h-index.
According to the definition of H-index on Wikipedia: "A scientist has index H. h of his/her N papers have at least h CIT Ations each, and the other n−h papers have no more than H citations each. "
For example, given citations = [3, 0, 6, 1, 5], which means the researcher has 5 papers into total and each of them had rece Ived 3, 0, 6, 1, 5 citations respectively. Since the researcher has 3 papers with at least 3 citations each and the remaining two with no further than 3 citations each, His h-index is 3.
Note:if There are several possible values for H, the maximum one is taken as the h-index.
This topic, prompted by the misleading, sorted, AP program is as follows:
public int Hindex (int[] citations) {
arrays.sort (citations);
int rst=0;
for (int i = citations.length-1 i >= 0; i--) {
if (citations[i) >= citations.length-i)
rst= CITATIONS.L ength-i;
}
return rst;
}
However, in fact, there are other solutions to this problem, using space to change time, you can use O (n) time complexity to solve this issue. The procedure is as follows:
public int Hindex (int[] citations) {
int len = citations.length;
Int[] Count = new Int[len + 1];
for (int c:citations)
if (C > Len)
count[len]++;
else
count[c]++;
int total = 0;
for (int i = len; I >= 0; i--) {Total
= count[i];
If (total >= i) return
i;
}
return 0;
}
H-index II
Follow up to H-index:what if the citations array is sorted into ascending order? Could you optimize your algorithm?
Compared to the H-index I, the given array is already sorted.
So, direct two-point search can be.
public class Solution {public
int hindex (int[] citations) {
if (citations = null | | citations.length = 0) return 0;
int L = 0, r = citations.length;
int n = citations.length;
while (L < r) {
int mid = L + (r-l)/2;
if (citations[mid] = = N-mid) return n-mid;
if (Citations[mid] < Citations.length-mid) L = mid + 1;
else R = Mid;
}
Return n-l
}
}