153. Find Minimum in Rotated Sorted Array | LeetCode Using Python
In this video, I tackle the problem of finding the minimum element in a rotated sorted array using Python. By employing a binary search approach, I demonstrate how to efficiently locate the smallest element in O(log n) time, even when the array is rotated.
Python Code:
class Solution:
def findMin(self, nums: List[int]) -> int:
#***You must write an algorithm that runs in O(log n) time.***
#Binary Search - Time complete of O(log n); Space Complexity of O(1)
left_pointer, right_pointer = 0, len(nums) -1
while left_pointer < right_pointer:
mid_point = (right_pointer + left_pointer) // 2
if nums[mid_point] > nums[right_pointer]:
left_pointer = mid_point + 1
else:
right_pointer = mid_point
return nums[left_pointer]
#Given the sorted rotated array nums of unique elements, return the minimum element of this array.