Find Minimum in Rotated Sorted Array II
Find Minimum in Rotated Sorted Array II is LeetCode 154 (Hard). An array sorted in non-decreasing order, so values may repeat, was rotated between 1 and n times; each rotation moves the last element to the front. Return the smallest value.
- LeetCode's follow-up asks whether repeated values change the running time, and why. They do: the worst case grows from O(log n) to O(n), and no algorithm can avoid that.
- The array holds at most 5000 values, from −5000 to 5000.
- n == nums.length
- 1 <= n <= 5000
- -5000 <= nums[i] <= 5000
- nums is sorted and rotated between 1 and n times.
Intuition
With distinct values, comparing the middle with the right end of the range always shows which side of the drop the middle is on. Repeats add a third outcome: the middle can equal the right end, and then it could be on either side. In [2, 2, 2, 0, 2] the 0 is right of the middle; in [2, 0, 2, 2, 2] it is left of it. Both show a 2 at the middle and a 2 at the right end.
On a tie, one move is still safe: drop just the right end. An equal copy of its value stays at the middle, so the smallest value cannot be lost. The price is that a tie shrinks the range by one, not by half.
A rotated sorted array that may repeat values. Expect the same binary search as the distinct version plus a case for equal values, and a worst case of O(n).
Approach
Before reading on, find two arrays with the same middle value and the same right-end value whose minimums sit on opposite sides of the middle. What can you safely throw away when you see that tie? Expect O(n) in the worst case.
Compare the middle with the right end
Keep a range lo to hi, starting with the whole array, and look at mid. The comparison of nums[mid] with nums[hi] now has three outcomes, and each one gets its own move.
Middle bigger: the drop is to the right
Values from the drop to hi never exceed nums[hi], so a middle bigger than nums[hi] must come before the drop. Set lo = mid + 1; nothing from lo to mid can be the minimum.
Middle smaller: keep the middle
Every value before the drop is at least every value after it, so a middle smaller than nums[hi] must come after the drop. The minimum is at mid or to its left: set hi = mid.
Tie: drop only the right end
When nums[mid] == nums[hi], set hi -= 1. If nums[hi] was the minimum, the same value still sits at mid, which stays in the range. Nothing larger can be ruled out, so this is the only safe move.
Return when one index is left
Loop while lo < hi and return nums[lo]. Every move keeps the smallest value inside the range, so the last value standing is it.
Code in Python, C++ and Java
lo to 2.lo to 3.lo and hi meet at index 3, so the answer is 0. 1 of the 3 steps were ties (the orange tiles), and each of those removed one index instead of half the range.hi becomes 1.lo to 1.lo and hi meet at index 1, so the answer is 0. 1 of the 3 steps were ties (the orange tiles), and each of those removed one index instead of half the range.lo and hi meet at index 0, so the answer is 3. 3 of the 3 steps were ties (the orange tiles), and each of those removed one index instead of half the range.Common pitfalls
Treating a tie as "go left"
if nums[mid] > nums[hi]:
lo = mid + 1
else:
hi = midelif nums[mid] < nums[hi]:
hi = mid
else:
hi -= 1This is the distinct-values code, and it assumes a tie means the middle is past the drop. On [2, 2, 2, 0, 2] it cuts the range to [2, 2, 2] and returns 2 instead of 0.
Dropping the left end on a tie
else:
lo += 1else:
hi -= 1The tie says nothing about nums[lo], and it may be the only copy of the minimum. On the unrotated [1, 2, 2, 2, 2] the first tie throws the 1 away and the search returns 2.
Edge cases
[3, 3, 3, 3]Every comparison is a tie, so hi steps down one index at a time until it meets lo, and the answer is 3. This is the slowest kind of input: n − 1 steps.
Complexity
Find Minimum in Rotated Sorted Array II FAQ
Can this also return where the rotation happened?
Not as written. On [1, 1, 2, 1] the ties walk hi down to index 0, which holds the smallest value, but the drop is at index 3. To get the drop, check before stepping on a tie: if nums[hi - 1] > nums[hi], then hi is the drop, so return it.
Why not just use min(nums)?
It is O(n) every time and passes the tests. The binary search is still worth it: it is O(log n) whenever ties are rare, and it only falls back to linear work on inputs that force any method to read most of the array.
How does this differ from the version with distinct values?
Only by the tie case. Find Minimum in Rotated Sorted Array never sees nums[mid] == nums[hi] inside the loop, so it always halves and stays O(log n).