philosyang.com

153. Find Minimum in Rotated Sorted Array

 1class Solution:
 2    def findMin(self, nums: List[int]) -> int:
 3        l = 0
 4        r = len(nums) - 1
 5
 6        while l < r:  # we can't `l <= r` if we do `r = m` below
 7            m = (l + r) // 2
 8
 9            if nums[m] > nums[r]:
10                # only when m > r do we know it's in (m, r]
11                # e.g., [1,2,3,4,0]
12                l = m + 1
13            else:
14                # if m < r, we only know it's in [l, m]
15                # e.g., [0,1,2,3,4]
16                # e.g., [3,4,0,1,2]
17                r = m
18
19        return nums[l]  # l === r here

#Neetcode150 #Binary-Search #Python