📘
Find Minimum in Rotated Sorted Array
MediumBinary Search
Given a sorted rotated array of unique elements nums, return the minimum element.
The array was originally sorted in ascending order, then rotated between 1 and n times. You must solve it in O(log n) time.
Example:
Input:
3,4,5,1,2
Output:
1
Reasoning:
- The input array is
3,4,5,1,2, which was originally sorted in ascending order and then rotated. - We use a modified binary search algorithm to find the minimum element in O(logn) time, where n is the number of elements in the array.
- The algorithm compares the middle element with the rightmost element: since 5>2, the minimum element must be in the right half of the array.
- We repeat this process with the right half
1,2and find that the minimum element is 1, which is the final output.
Constraints:
- 1 <= len(nums) <= 5000
- -5000 <= nums[i] <= 5000
- All values are unique
- nums was sorted then rotated
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.