Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
leetcode-array
class Solution:
def findMin(self, nums: List[int]) -> int:
def find_pivot_element(nums, l, r):
mid = l + (r-l) // 2
if r-l <= 1:
return min(nums[l], nums[r])
if nums[l] < nums[r]:
return nums[l]
left_min = find_pivot_element(nums, l, mid-1)
right_min = find_pivot_element(nums, mid, r)
return min(left_min, right_min)
n = len(nums)
return find_pivot_element(nums, 0, n-1)
def findMin(self, nums: List[int]) -> int:
def find_pivot_element(nums, l, r):
while l < r:
m = l + (r-l) // 2
if nums[m-1] > nums[m]:
return nums[m]
if nums[m] > nums[0]:
l = m + 1
else:
r = m
return nums[l]
n = len(nums)
# to avoid it's fully sorted already
if nums[0] <= nums[n-1]:
return nums[0]
return find_pivot_element(nums, 0, n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment