3 Lines O(n) Solution With Graph Explanation


  • 0
    S
    public int hIndex(int[] citations) {
        for (int h = citations.length; h > 0; h--)
            if (citations[citations.length - h] >= h) return h;
        return 0;
    }
    

    Explanation:

    If citations is in descending order, you can plot it as follows. The intersection of citations and y = x is the h-index.
    alt text

    Now, the input is given in ascending order. What you need to do is just properly "translate" the array index you visit.

    The image credits to the editorial solution of H-Index One.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.