153. Find Minimum in Rotated Sorted Array
Description
Suppose an array of length n
sorted in ascending order is rotated between 1
and n
times. For example, the array nums = [0,1,2,4,5,6,7]
might become:
[4,5,6,7,0,1,2]
if it was rotated4
times.[0,1,2,4,5,6,7]
if it was rotated7
times.
Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]]
1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]]
.
Given the sorted rotated array nums
of unique elements, return the minimum element of this array.
Example 1:
Example 2:
Example 3:
Constraints:
n == nums.length
1 <= n <= 5000
-5000 <= nums[i] <= 5000
All the integers of
nums
are unique.nums
is sorted and rotated between1
andn
times.
Tags
Array, Binary Search
Solution
Perform binary search to find the precipice in the array, by comparing nums[mid]
with the last element of the array. If nums[mid]
is larger then move the left pointer to the middle index, otherwise move the right pointer. Finally, we return the smaller one of both nums[start]
and nums[end]
.
Complexity
Time complexity:
Space complexity:
Code
Last updated
Was this helpful?