Find Minimum in Rotated Sorted Array
LeetCode 153 · Medium · solution, intuition & step-by-step walkthrough
nums[mid] < nums[high] ⇒ high = mid, else low = mid + 1.
This is how I solved Find Minimum in Rotated Sorted Array (LeetCode 153), a medium problem from the Rotated Sorted Array pattern. Below: the intuition, the approach step by step, my accepted Python code, animated test cases and a line-by-line debugger.
Intuition
Compare mid with the right end. If nums[mid] < nums[high], the right half is sorted and the minimum is at mid or to its left; otherwise the drop — and the minimum — is to the right of mid.
Approach
- 1
low < high loop.
- 2
Right half sorted → high = mid (mid may be the min).
- 3
Otherwise low = mid + 1.
- 4
Return nums[low].
How I solved it
Binary search on the dropPythonO(log n) time
class Solution:
def findMin(self, nums: List[int]) -> int:
#find mini in which half
low = 0
high = len(nums)-1
while low<high:
mid = (low+high)//2
if nums[mid]<nums[high]:#remove the right half
high = mid
else:
low = mid+1 #remove the left half
return nums[low]
Find Minimum in Rotated Sorted Array — FAQ
What is the intuition behind Find Minimum in Rotated Sorted Array?
Compare mid with the right end. If nums[mid] < nums[high], the right half is sorted and the minimum is at mid or to its left; otherwise the drop — and the minimum — is to the right of mid.
How do you solve Find Minimum in Rotated Sorted Array (LeetCode 153)?
1. low < high loop. 2. Right half sorted → high = mid (mid may be the min). 3. Otherwise low = mid + 1. 4. Return nums[low].
What is the time and space complexity of Find Minimum in Rotated Sorted Array?
Time: O(log n). Space: O(1). (Accepted approach: Binary search on the drop.)
What are common mistakes in Find Minimum in Rotated Sorted Array?
Compare with high, not low — comparing with low fails on an unrotated array.