binary search August 4, 2022

Find minimum in rotated sorted array

Time O(log(n)) Space O(1) Open original problem

We will start by assuming the minimum number is fisrt element of the list. Then we will do a binary search, but before calculating the mid value, we will check, if the value at left pointer is less that value of right pointer. In that case, this part of the list is already sorted, and we can just pick the minimum value from here. Otherwise, we will run our regular binary search iteration.

class Solution:
    def findMin(self, nums: List[int]) -> int:
        res = nums[0]
        l, r = 0 , len(nums)-1
        while l <= r:
            if nums[l] < nums[r]:
                res = min(res, nums[l])
                break
            m = (l + r) // 2
            res = min(res, nums[m])
            if nums[m] >= nums[l]:
                l = m + 1
            else:
                r = m - 1
        return res

Time Complexity: O(log(n))
Space Complexity: O(1)