題目:Given an array of citations (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.”
For example, given citations = [3, 0, 6, 1, 5], which means the researcher has 5 papers in total and each of them had received 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 more 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.
本題是要求一個科學家的H_Index。也就是論文索引的一個名額。有h_index的定義可知,其值一定在0~citations範圍内。是以我們可以使用一個數組來儲存論文的索引次數。代碼入下,可以擊敗58%的使用者。
public static int hIndex(int[] citations) {
int n = citations.length, tot=;
//arr用來儲存每個索引次數論文的數量。arr[0]就是索引次數為0的論文數
//其長度比citations大一,是因為arr[n]用來儲存索引次數大于n的論文數,因為索引次數大于n的文章一定是滿足h_index的文章。
int[] arr = new int[n+];
for (int i=; i<n; i++) {
//周遊citations數組,并将論文的索引資訊儲存到arr數組中
if (citations[i]>=n) arr[n]++;
else arr[citations[i]]++;
}
//為了求最大的h_index,是以倒叙周遊arr,直到找到滿足條件的索引i并傳回。
for (int i=n; i>=; i--) {
tot += arr[i];
if (tot>=i) return i;
}
return ;
}
當然除此之外也可以先把數組進行排序,但相比上面方法的兩次循環而言效率反倒有所下降,主要是因為排序算法的效率是o(nlogn),而上面代碼的效率是o(n)。代碼入下:
public int hIndex(int[] citations) {
if (citations == null || citations.length ==) return;
Arrays.sort(citations);
int len = citations.length;
for (int i =; i < citations.length; i++) {
if (len <= citations[i])
return len;
else
len--;
}
return len;
}
此外,還可以采用排序之後在進行二分搜尋的方式,代碼入下:
public int hIndex(int[] citations) {
Arrays.sort(citations);
int n = citations.length;
int i = , j = n - ;
while (i <= j) {
int k = (i + j) / ;
int v = citations[k];
int h = n - k;
if (v >= h) {
j = k - ;
} else {
i = k + ;
}
}
return n - j - ;
}