153_Find Minimum in Rotated Sorted Array
[Medium][Array, Binary Search]
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., [0,1,2,4,5,6,7]
might become [4,5,6,7,0,1,2]
). Find the minimum element. You may assume no duplicate exists in the array.
Example 1:
Example 2:
Solution:
Idea: Binary Search
If middle >= 1 and nums[middle-1] > nums[middle], then nums[middle] is the smallest element.
If nums[left] > nums[middle], the minimum element must be in the left part.
If nums[middle] > nums[right], the minimum element must be in the right part.
Otherwise, every element is in ascending order, the minimum element is the leftmost element.
Time Complexity:
Space Complexity:
Last updated