Find Minimum in Rotated Sorted Array II (LeetCode 154): Solution and Explanation
A sorted array was rotated, so it rises, drops once, and rises again. It may contain duplicates. Return the smallest value, using as few steps as possible.
Intuition
Picture the rotated array as two rising ramps with a cliff between them. The minimum sits at the bottom of the cliff. Every element on the left ramp is at least as large as every element on the right ramp, and the last element nums[hi] belongs to the right ramp.
That makes nums[hi] a good reference. If nums[mid] > nums[hi], mid is on the left ramp, so the cliff and the minimum are strictly to its right. If nums[mid] < nums[hi], mid is on the right ramp, so the minimum is at mid or before it. Either way half the window goes away, just like in the version without duplicates.
Duplicates create the third case: nums[mid] == nums[hi]. Now mid could be on either ramp. In [3,3,1,3,3,3,3] the cliff is left of mid, while in [3,3,3,3,1,3,3] it is right of mid, and both have the same mid and hi values. You cannot throw away a half. You can throw away one element, though: hi is not needed, because mid holds the same value and stays in the window. So hi -= 1 is safe, and it is what makes the worst case O(n) instead of O(log n).
Approach: Binary search with a shrinking tie case
- Set
lo = 0andhi = n - 1. - While
lo < hi, computemid = (lo + hi) // 2. - If
nums[mid] > nums[hi], the minimum is to the right of mid:lo = mid + 1. - If
nums[mid] < nums[hi], the minimum is at mid or to its left:hi = mid. - If they are equal, you cannot tell which side the drop is on, so shrink by one:
hi -= 1. - When the loop ends,
lo == hiandnums[lo]is the minimum.
Step-by-step example
nums = [1,3,5]
- Start with
lo = 0,hi = 2. mid = 1.nums[1] = 3 < nums[2] = 5, so the range mid..hi is sorted and the minimum is at mid or left of it:hi = 1.mid = 0.nums[0] = 1 < nums[1] = 3, sohi = 0.- Now
lo == hi == 0, and the answer isnums[0] = 1.
Code: Python, Java, C++
1class Solution:
2 def findMin(self, nums: List[int]) -> int:
3 lo, hi = 0, len(nums) - 1
4 while lo < hi:
5 mid = (lo + hi) // 2
6 if nums[mid] > nums[hi]:
7 lo = mid + 1
8 elif nums[mid] < nums[hi]:
9 hi = mid
10 else:
11 hi -= 1
12 return nums[lo]1class Solution {
2 public int findMin(int[] nums) {
3 int lo = 0, hi = nums.length - 1;
4 while (lo < hi) {
5 int mid = lo + (hi - lo) / 2;
6 if (nums[mid] > nums[hi]) {
7 lo = mid + 1;
8 } else if (nums[mid] < nums[hi]) {
9 hi = mid;
10 } else {
11 hi--;
12 }
13 }
14 return nums[lo];
15 }
16}1class Solution {
2public:
3 int findMin(vector<int>& nums) {
4 int lo = 0, hi = nums.size() - 1;
5 while (lo < hi) {
6 int mid = lo + (hi - lo) / 2;
7 if (nums[mid] > nums[hi]) {
8 lo = mid + 1;
9 } else if (nums[mid] < nums[hi]) {
10 hi = mid;
11 } else {
12 hi--;
13 }
14 }
15 return nums[lo];
16 }
17};Time and space complexity
O(log n) average, O(n) worst
Each strict comparison halves the window. A tie only removes one element, so an array of mostly equal values, like [1,1,1,1,0,1,1], can need up to n steps.
O(1)
Only the three indices are stored.
Other approaches
Linear scan (time O(n), space O(1))
Return min(nums) or scan for the first element smaller than the one before it. It is always O(n), which matches the worst case of the binary search but loses the O(log n) behavior on typical inputs.
Divide and conquer (time O(log n) average, O(n) worst, space O(log n))
Recursively take the minimum of both halves, stopping early on any half whose first element is smaller than its last, since that half is sorted. It also degrades to O(n) with many duplicates and uses recursion stack space.
Common mistakes
- Comparing
nums[mid]withnums[lo]instead ofnums[hi]. With lo, a sorted (rotated n times) array and a rotated one can look the same, and the logic gets messy. - Moving
lo = mid + 1orhi = mid - 1on a tie. Either can skip past the minimum. Onlyhi -= 1is safe. - Setting
hi = mid - 1whennums[mid] < nums[hi]. Mid itself may be the minimum, so keep it withhi = mid. - Using
while lo <= hi, which can loop forever withhi = mid.
Frequently asked questions
How does this differ from Find Minimum in Rotated Sorted Array (no duplicates)?
The only change is the tie case. Without duplicates, nums[mid] can never equal nums[hi] when mid < hi, so the search always halves. With duplicates, you add the hi -= 1 branch.
Why is dropping hi safe when nums[mid] == nums[hi]?
If nums[hi] were the minimum, nums[mid] is an equal value still inside the window, so the minimum value is not lost. The function returns a value, not an index, so either copy is fine.
Can the worst case be better than O(n)?
No. In an array of all 1s with a single 0 hidden somewhere, any algorithm must look at nearly every element to find the 0, so O(n) is unavoidable in the worst case.