Skip to content

Instantly share code, notes, and snippets.

@PeculiarE
Last active March 8, 2026 22:28
Show Gist options
  • Select an option

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

Select an option

Save PeculiarE/acb248e83ef968fec666768dae1311f6 to your computer and use it in GitHub Desktop.
Binary Search - LeetCode - Day 67

Question

Intuition

Classic binary search problem

Approach

Complexity

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

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

Code

class Solution:
    def search(self, nums: List[int], target: int) -> int:
        low, high = 0, len(nums) - 1
        while (low <= high):
            mid = (low + high) // 2
            if nums[mid] == target:
                return mid
            elif nums[mid] > target:
                high = mid - 1
            else:
                low = mid + 1
        return -1
        

Result

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