Skip to content

Grind 75 - Binary Search

Posted on:August 4, 2023 at 12:55 AM at 1 min read

Solution for Binary Search problem which is a part of Grind 75 list.

class Solution:
    def search(self, nums: List[int], target: int) -> int:
        low = 0
        high = len(nums) - 1

        while low <= high:
            mid = (low + high) // 2

            if target == nums[mid]:
                return mid

            if target > nums[mid]:
                low = mid + 1

            if target < nums[mid]:
                high = mid - 1

        return -1

Comments