Intuition

  • Simply imagine where you are falling off, and then where mid is located corresponding to where you are falling off.
  • Also keep in mind the array of size 1, where if you are in this subproblem, left could be < right, in which case you just return nums[left] or nums[right] since there is only one element left.

Runtime

due to binary search

Solution

Elegant

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

Verbose

class Solution:
    def findMin(self, nums: List[int]) -> int:
        left = 0
        right = len(nums) - 1
 
        while left <= right:
            mid = (left + right) // 2
 
            if nums[mid - 1] > nums[mid]:
                return nums[mid]
 
            if nums[left] > nums[right]: # there is a pivot
                if nums[mid] >= nums[left]:
                    left = mid + 1
                elif nums[mid] < nums[left]:
                    right = mid - 1
            else: # straight line
                right = mid - 1
 
        return nums[left]

Notes


References

https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/