find-minimum-in-rotated-sorted-array.py
binary-search/find-minimum-in-rotated-sorted-array.py · Python · 622 B · 2026-02-14 18:26
# Binary search. Or brute force. # binary-search # Binary search - O(log n) time, O(1) space def findMin(nums: list[int]) -> int: """ >>> findMin([3,4,5,1,2]) 1 >>> findMin([4,5,6,7,0,1,2]) 0 """ l = 0 r = len(nums) -1 while l < r: mid = l + (r-l) // 2 if nums[mid] > nums[r]: # the smallest number is at the right of mid l = mid+1 else: # the smallest number is at the left of mid r = mid return nums[l] # Brute force - O(n) time, O(1) space def findMin1(nums: list[int]) -> int: return min(nums)