Binary search solution:
class Solution:
def findMin(self, nums: List[int]) -> int:
left, right = 0, len(nums) - 1
while left < right:
mid = left + (right - left) // 2
if nums[mid] > nums[right]:
left = mid + 1
else:
right = mid
return nums[right]
Divide and conquer solution:
class Solution:
def findMin(self, nums: List[int]) -> int:
def helper(left, right):
if nums[left] <= nums[right]:
return nums[left]
mid = left + (right - left) // 2
return min(helper(left, mid), helper(mid+1, right))
return helper(0, len(nums) - 1)
No comments:
Post a Comment