Skip to content

Instantly share code, notes, and snippets.

@ThomasHigginson
Created April 6, 2022 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThomasHigginson/ac74834ea5874ac199c937ae2704bb79 to your computer and use it in GitHub Desktop.
Save ThomasHigginson/ac74834ea5874ac199c937ae2704bb79 to your computer and use it in GitHub Desktop.
def findMin(self, nums: List[int]) -> int:
if len(nums) == 1 or nums[0] < nums[len(nums)-1]:
return nums[0] # Our two base cases
left, right = 0, len(nums)-1
while left <= right:
mid = (left + right) // 2
if nums[mid] > nums[mid+1]: # Case 1 Inflection Point
return nums[mid+1]
if nums[mid] < nums[mid-1]: # Case 2 Inflection Point
return nums[mid]
if nums[left] < nums[mid]: # Case 2 which side to search
left = mid + 1
else:
right = mid - 1
return -1 # Should never be the case
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment