H-Index II

Problem: H-Index II

We can use binary search to solve this problem. We first fetch index of middle and element of middle. If number of elements right to middle is smaller than middle, the number can be h-index while it can be also bigger by adjusting pointer to left. However, if bigger, than the elements right to middle need to be less so we need to adjust pointer left.

Code in Python:

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

results matching ""

    No results matching ""