Algorithm Day68 - Find Minimum in Rotated Sorted Array
π§© Problem 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 rotated4
times.[0,1,2,4,5,6,7]
if rotated7
times.
Given the rotated sorted array nums
of unique elements, return the minimum element of this array.
You must write an algorithm with O(log n) runtime complexity.
π¬ Examples
Example 1
1 | Input: nums = [3,4,5,1,2] |
Example 2
1 | Input: nums = [4,5,6,7,0,1,2] |
Example 3
1 | Input: nums = [11,13,15,17] |
π‘ Intuition
The minimum value is the βpivot pointβ where rotation happens.
- If
nums[mid] > nums[right]
, the minimum lies in the right half. - Otherwise, it lies in the left half (including mid).
This ensures O(log n) with binary search.
π’ Java Code (Binary Search)
1 | class Solution { |
β± Complexity Analysis
- Time: O(log n) β binary search.
- Space: O(1) β constant space.
βοΈ Summary
- Use binary search to find the pivot (minimum element).
- Compare
nums[mid]
withnums[right]
to decide which half to search.
Related problems
lc-33
β Search in Rotated Sorted Arraylc-154
β Find Minimum in Rotated Sorted Array II