Guess Number Higher or Lower

Problem Id: 374 Difficulty: Easy


Intuition

Solution


class Solution(object):
    def guessNumber(self, n):
        """
        :type n: int
        :rtype: int
        """
        if guess(1) == 0:
            return 1
        if guess(n) == 0:
            return n

        lo = 1
        hi = n
        while hi > lo:
            mid = (hi + lo) // 2
            tmp = guess(mid)
            if tmp < 0:
                hi = mid
            elif tmp > 0:
                lo = mid
            else:
                return mid
        return hi