Created
April 6, 2022 18:47
-
-
Save ThomasHigginson/ac74834ea5874ac199c937ae2704bb79 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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