Skip to content

Instantly share code, notes, and snippets.

@PeculiarE
Created March 6, 2026 22:28
Show Gist options
  • Select an option

  • Save PeculiarE/54dfdc4919e8e22d1cb84f340860efa4 to your computer and use it in GitHub Desktop.

Select an option

Save PeculiarE/54dfdc4919e8e22d1cb84f340860efa4 to your computer and use it in GitHub Desktop.
Guess Number Higher Or Lower - LeetCode - Day 65

Question

Intuition

Approach

Complexity

  • Time complexity: $$O(log \ n)$$

  • Space complexity: $$O(1)$$

Code

# 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:
        low, high = 1, n
        while (low <= high):
            mid = (low + high) // 2
            myGuess = guess(mid)
            if myGuess == 0:
                return mid
            elif myGuess == -1:
                high = mid - 1
            else:
                low = mid + 1
        

Result

Screenshot 2026-03-06 at 22 25 32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment