Skip to content
LC-0374 Easy LeetCode

374. Guess Number Higher or Lower

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 56% Topics: Binary Search, Interactive
View full problem on LeetCode

Reading material

Reference solution (spoiler · python)
# Time:  O(logn)
# Space: O(1)

class Solution(object):
    def guessNumber(self, n):
        """
        :type n: int
        :rtype: int
        """
        left, right = 1, n
        while left <= right:
            mid = left + (right - left) / 2
            if guess(mid) <= 0: # noqa
                right = mid - 1
            else:
                left = mid + 1
        return left

Solution from kamyu104/LeetCode-Solutions · MIT