Binary Search to solve it, python 38ms


  • 2
    M
    def hIndex(self, citations):
        """
        :type citations: List[int]
        :rtype: int
        """
        if len(citations) == 0 or not citations:
            return 0
        low, high, length = 0, len(citations)-1, len(citations)
        while low <= high:
            mid = (low + high) // 2
            if citations[mid] < length - mid:
                low = mid + 1
            else:
                high = mid - 1
        return length - high - 1

  • 1

    Great code! The logic may be a little bit simplified (the idea is taken from here) :-)

    class Solution(object): 
        def hIndex(self, citations):
            """
            :type citations: List[int]
            :rtype: int
            """
            n = len(citations)
            l, r = 0, n - 1
            while l <= r:
                m = (l + r) // 2
                if citations[m] >= n - m:
                    r = m - 1
                else:
                    l = m + 1
            return n - r - 1
    

  • 0
    M

    awesome, his idea inspries me


Log in to reply
 

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