LeetCode #154 Hard

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.
Constraints
  • n == nums.length
  • 1 <= n <= 5000
  • -5000 <= nums[i] <= 5000
  • nums is sorted and rotated between 1 and n times.
binary-searcharrays
Open on LeetCode ↗
02

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.

How to spot this pattern

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).

03

Approach

Try it first

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.

1

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.

2

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.

3

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.

4

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.

5

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.

04

Code in Python, C++ and Java

▶1from typing import List
▶2 
▶3 
▶4class Solution:
▶5 def findMin(self, nums: List[int]) -> int:
▶6 lo, hi = 0, len(nums) - 1
▶7 while lo < hi:
▶8 mid = (lo + hi) // 2
▶9 if nums[mid] > nums[hi]:
▶10 lo = mid + 1
▶11 elif nums[mid] < nums[hi]:
▶12 hi = mid
▶13 else:
▶14 hi -= 1
▶15 return nums[lo]
array20lo21220324hicomparemiddleright endrange is the whole array
lo0
hi4
Each step compares the middle of the range with its right end. With repeated values that comparison can come out equal, and an equal result is the case this problem is about.
array20lo2122mid0324hicompare22=middleright endmiddle 2 = right end 2 → drop only the right end
mid2
nums[mid]2
nums[hi]2
hi3
A tie: the drop could be on either side of the middle, so neither half can go. Dropping index 4 alone is safe, because the same value 2 is still at index 2. The range shrinks by one, not by half.
array20lo21mid2203hi24compare20>middleright endmiddle 2 > right end 0 → minimum is right of it
mid1
nums[mid]2
nums[hi]0
lo2
Values after the drop never exceed the right end, 0. The middle, 2, is bigger, so it comes before the drop, along with everything from index 0 to 1. Move lo to 2.
array202122lo mid03hi24compare20>middleright endmiddle 2 > right end 0 → minimum is right of it
mid2
nums[mid]2
nums[hi]0
lo3
Values after the drop never exceed the right end, 0. The middle, 2, is bigger, so it comes before the drop, along with everything from index 2 to 2. Move lo to 3.
array20212203lo hi24comparemiddleright endone index left → minimum is 0
lo = hi3
answer0
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.
05

Common pitfalls

Treating a tie as "go left"

✗ Wrong
if nums[mid] > nums[hi]:
    lo = mid + 1
else:
    hi = mid
✓ Right
elif nums[mid] < nums[hi]:
    hi = mid
else:
    hi -= 1

This 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

✗ Wrong
else:
    lo += 1
✓ Right
else:
    hi -= 1

The 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.

06

Edge cases

Every value the same, e.g. [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.

07

Complexity

Time
O(log n) typical, O(n) worst
Space
O(1)
Without ties each step halves the range. Each tie removes one index, so an array of equal values with one smaller value hidden among them can take close to n steps. No method can do better: until the hidden value is found, every unread position could hold it.
08

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).