Find Minimum in Rotated Sorted Array II – Python 3 ( Week 11 – 06)

class Solution:
    def findMin(self, nums: List[int]) -> int:
        min_num = nums[0]
        
        start, end = 0, len(nums) - 1
        
        while start + 1 < end:
            mid = (start + end) // 2
            if nums[mid] < nums[end]:
                end = mid
            elif nums[mid] > nums[end]:
                start = mid
            else:
                end = end - 1
        
        if nums[start] < nums[end]:
            return nums[start]
        else:
            return nums[end]
            
            
            

Time O(logn). Space O(n).

Leave a comment

Design a site like this with WordPress.com
Get started