H-Index II

Given an array of citations sorted in ascending order (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 citations each."

Example:

Input: citations = [0,1,3,5,6]
Output: 3 
Explanation: [0,1,3,5,6] means the researcher has 5 papers in total and each of 
them had received 0, 1, 3, 5, 6 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,
her h-index is 3.

Note:

If there are several possible values for h, the maximum one is taken as the h-index.

 

class Solution {
    public int hIndex(int[] citations) {
        int length = citations.length;

    int low = 0, high = length - 1, mid;

    while (low <= high) {
        mid = (low + high) / 2;

        if (length - mid > citations[mid])
            low = mid + 1;
        else
            high = mid - 1;
    }

    return length - low;
    }
}

Try it on Leetcode

Formally, if f is the function that corresponds to the number of citations for each publication, we compute the h-index as follows: First we order the values of f from the largest to the lowest value. Then, we look for the last position in which f is greater than or equal to the position (we call h this position). For example, if we have a researcher with 5 publications A, B, C, D, and E with 10, 8, 5, 4, and 3 citations, respectively, the h-index is equal to 4 because the 4th publication has 4 citations and the 5th has only 3. In contrast, if the same publications have 25, 8, 5, 3, and 3 citations, then the index is 3 because the fourth paper has only 3 citations.

f(A)=10, f(B)=8, f(C)=5, f(D)=4, f(E)=3 → h-index=4
f(A)=25, f(B)=8, f(C)=5, f(D)=3, f(E)=3 → h-index=3
[Above explanation is taken from H-index]

From above explanation, it is easy to understand that,[citation sorted in descending order] if index position is greater than or equal to citation then particular position is H-index. We can easily find by iterating each element but it takes O(N). But we solved using O(log N).

We are going to find a position by subtracting low or mid from total length.

1) If length-mid is greater than citation then, increase mid by 1 and store it in low.
2) Else, decrease mid by 1 and store it in high
3) Repeat above steps until low is greater than high.
4) Finally return length-low, which will be h-index.

Time Complexity: O(log N)

🎉🎉    🎈🎈   ✨✨    🎊🎊     🌟🌟     🎉🎉    🎈🎈   ✨✨    🎊🎊     🌟🌟  🎉🎉    🎈🎈   ✨✨    🎊🎊    

Also, it is happy to say this is our 100th post. It is not easy to reach this position without your support. Please support us and share your views in comment section. Thank you so much to all of our followers and supporters.

🎉🎉    🎈🎈   ✨✨    🎊🎊     🌟🌟     🎉🎉    🎈🎈   ✨✨    🎊🎊     🌟🌟   🎉🎉    🎈🎈   ✨✨    🎊🎊  

Click Follow button to receive updates from us instantly.

Please let us know about your views in comment section.

Happy Coding...😍

Comments

Popular posts from this blog

Balanced Binary Tree

First Unique Character in a String

Majority Element

Smallest Range II