Find Minimum in Rotated Sorted Array II (LeetCode 154): Solution and Explanation

Hard
Array
Binary search

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

  1. Set lo = 0 and hi = n - 1.
  2. While lo < hi, compute mid = (lo + hi) // 2.
  3. If nums[mid] > nums[hi], the minimum is to the right of mid: lo = mid + 1.
  4. If nums[mid] < nums[hi], the minimum is at mid or to its left: hi = mid.
  5. If they are equal, you cannot tell which side the drop is on, so shrink by one: hi -= 1.
  6. When the loop ends, lo == hi and nums[lo] is the minimum.

Step-by-step example

nums = [1,3,5]
  1. Start with lo = 0, hi = 2.
  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.
  3. mid = 0. nums[0] = 1 < nums[1] = 3, so hi = 0.
  4. Now lo == hi == 0, and the answer is nums[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]
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

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.

Space

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] with nums[lo] instead of nums[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 + 1 or hi = mid - 1 on a tie. Either can skip past the minimum. Only hi -= 1 is safe.
  • Setting hi = mid - 1 when nums[mid] < nums[hi]. Mid itself may be the minimum, so keep it with hi = mid.
  • Using while lo <= hi, which can loop forever with hi = 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.