LeetCode ยท #153
Find Minimum in Rotated Sorted Array Solution
Given a rotated sorted array of unique elements, find the minimum element in O(log n).
01
Section One ยท Problem
Problem Statement
A sorted array of unique elements was rotated between 1 and n times. Find the minimum element. The original array was sorted in ascending order.
Example
Input: nums = [3,4,5,1,2]
Output: 1 // Originally [1,2,3,4,5], rotated 3 times
Constraints
โข n == nums.length โข 1 โค n โค 5000 โข All elements unique โข nums was sorted ascending then rotated
02
Section Two ยท Approach 1
Linear Scan โ O(n)
Scan the array and track the minimum. Or find the first element smaller than its predecessor โ that's the rotation point.
Problem: O(n). Binary search on the rotation structure gives O(log n) โ compare mid to the right boundary to decide which half contains the minimum.
| Metric | Value |
|---|---|
| Time | O(n) |
| Space | O(1) |
03
Section Three ยท Approach 2
Binary Search โ O(log n)
Compare nums[mid] to nums[right]. If nums[mid] > nums[right], the minimum is in the right half (the rotation break is there). Otherwise, it's in the left half (including mid).
๐ก Mental model: Imagine a hill with a cliff โ the array ascends, then drops to the minimum. If you're standing on the left slope (mid > right), the cliff is to your right. If you're on the right slope (mid โค right), the cliff is to your left or you're already past it. Walk toward the cliff.
Algorithm
- Step 1:
left = 0,right = n - 1. - Step 2: While
left < right: - Step 3: If
nums[mid] > nums[right]โ minimum is in[mid+1, right]โleft = mid + 1. - Step 4: Else โ minimum is in
[left, mid]โright = mid. - Step 5: Return
nums[left].
๐ฏ Why compare to right, not left?:
- Comparing to left is ambiguous:
nums[left] โค nums[mid]doesn't tell you if the minimum is left or right (could be an unrotated array). - Comparing to right is definitive: if
nums[mid] > nums[right], there must be a drop between mid and right.
04
Section Four ยท Trace
Visual Walkthrough
Trace through nums = [3, 4, 5, 1, 2]:
Binary Search โ find the rotation point (minimum)
05
Section Five ยท Implementation
Code โ Java & Python
Java โ Binary Search
class Solution {
public int findMin(int[] nums) {
int left = 0, right = nums.length - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] > nums[right])
left = mid + 1; // min is in right half else
right = mid; // min is in left half (incl. mid)
}
return nums[left];
}
}
Python โ Binary Search
class Solution:
def findMin(self, nums: list[int]) -> int:
left, right = 0, len(nums) - 1 while left < right:
mid = (left + right) // 2 if nums[mid] > nums[right]:
left = mid + 1 else:
right = mid
return nums[left]
06
Section Six ยท Analysis
Complexity Analysis
| Approach | Time | Space | Trade-off |
|---|---|---|---|
| Linear Scan | O(n) | O(1) | Simple but slow |
| Binary Search โ optimal | O(log n) | O(1) | Compare mid to right boundary |
07
Section Seven ยท Edge Cases
Edge Cases & Pitfalls
| Case | Input | Why It Matters |
|---|---|---|
| Not rotated | [1, 2, 3, 4] | nums[mid] always โค nums[right] โ right converges to 0. Min = nums[0]. |
| Single element | [1] | L==R immediately โ return nums[0]. |
| Two elements | [2, 1] | mid=0, nums[0]=2 > nums[1]=1 โ L=1. Return nums[1]=1. |
| Fully rotated | [2,3,4,5,1] | Min at last position. Binary search converges there. |
โ Common Mistake: Using
left โค right instead of left < right. This is a "find boundary" search, not "find exact target." With โค, the loop runs one extra time and can cause infinite loops when right = mid and left == right.