Leetcode 374 Solution

This article provides solution to leetcode question 374 (guess-number-higher-or-lower)

https://leetcode.com/problems/guess-number-higher-or-lower

Solution

# The guess API is already defined for you.
# @param num, your guess
# @return -1 if num is higher than the picked number
#          1 if num is lower than the picked number
#          otherwise return 0
# def guess(num: int) -> int:

class Solution:
    def guessNumber(self, n: int) -> int:
        l = 1
        r = n

        while l < r:
            m = (l + r) // 2

            if guess(m) == 0:
                return m
            elif guess(m) == 1:
                l = m + 1
            else:
                r = m - 1

        return l