Leetcode 274/275 H-index Java__java

Source: Internet
Author: User

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
    }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.