题目:
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.

Hint:

An easy approach is to sort the array first.
What are the possible values of h-index?
A faster approach is to use extra space.

解答:
满足这个index的最大值不会超过citations数组的len, 因为如果超过了,就不可能有这么多的paper数。所以brute force就是把所有可能的n个paper至少有n个citation的n记下来,然后找出最大的n。
代码:

public int hIndex(int[] citations) {
        int maxHIndex = 0;
        for (int i = citations.length; i >= 0; i--) {
            int citNum = i;
            int count = 0;
            for (int j = 0; j < citations.length; j++) {
                if (citations[j] >= citNum) {
                    count++;
                }
            }
            if (count >= citNum) {
                maxHIndex = Math.max(maxHIndex, citNum);
            }
        }
        
        return maxHIndex;
    }

Follow up是牺牲空间来换取时间,那就用另外一个数组来存当前index存在的文章数,然后从后往前相加,如果满足条件就输出这个最大的index。
举个例子:

citations: 3, 0, 6, 1, 5
arr: 0 1 2 3 4 5
val: 1 1   1   2

那么从后向前扫的时候,记一个count,扫到arr(5)的时候,count=2 < 5, 不满足;扫arr(4),count=2 < 4, 不满足;扫arr(3), count=2+1=3 == 3, 满足。因为是从后向前扫的,所以当前的index就是满足条件的最大数。
代码:

public int hIndex(int[] citations) {
        if (citations == null || citations.length == 0) return 0;
        
        int len = citations.length;
        int[] arr = new int[len + 1];
        for (int i = len - 1; i >= 0; i--) {
            //最大的index也不会大于数组的长度,所以,超过数组长度的citation可以都记在len里
            if (citations[i] > len) {
                arr[len] += 1;
            } else {
                //arr的index就是一篇文章中citation的个数,arr的值就是有这么多citation的文章的个数
                arr[citations[i]] += 1;
            }
        }
        
        int count = 0;
        for (int i = len; i >= 0; i--) {
            count += arr[i];
            if (count >= i) {
                return i;
            }
        }
        return 0;
    }

guoluona
199 声望14 粉丝

« 上一篇
75. Sort Colors
下一篇 »
164. Maximum Gap