Find Minimum in Rotated Sorted Array II
Scratchpad
Saved in this browser
Problem
Section titled “Problem”Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,4,4,5,6,7] might become:
[4,5,6,7,0,1,4]if it was rotated4times.[0,1,4,4,5,6,7]if it was rotated7times.
Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]].
Given the sorted rotated array nums that may contain duplicates, return the minimum element of this array.
You must decrease the overall operation steps as much as possible.
Example 1:
Input: nums = [1,3,5]Output: 1Example 2:
Input: nums = [2,2,2,0,1]Output: 0Constraints:
n == nums.length-
nums[i] numsis sorted and rotated between1andntimes.
Follow-up: This problem is similar to Find Minimum in Rotated Sorted Array, but nums may contain duplicates. Would this affect the runtime complexity? How and why?
Solution approach
Section titled “Solution approach”Intuition
Section titled “Intuition”Use the same binary search as in the version without duplicates: compare nums[mid] with the last element of the range, nums[hi], while keeping the invariant “the minimum value is inside [lo, hi]”.
nums[mid] > nums[hi]:midis in the left (larger) run, so the minimum is strictly right ofmid.nums[mid] < nums[hi]:mid..hiis ascending, so the minimum is atmidor to its left.nums[mid] == nums[hi]: now we cannot tell. In[3,3,1,3]the minimum is right ofmid, but in[3,1,3,3,3]it is left ofmid. What we do know is thatnums[hi]has a copy atmid, so dropping indexhicannot lose the minimum value. We shrink the range by one withhi--.
Algorithm
Section titled “Algorithm”- Set
lo = 0,hi = n - 1. - While
lo < hi, withmid = lo + (hi - lo) / 2:- if
nums[mid] > nums[hi], setlo = mid + 1; - else if
nums[mid] < nums[hi], sethi = mid; - else (equal) set
hi = hi - 1.
- if
- Return
nums[lo].
Walkthrough
Section titled “Walkthrough”[3,3,1,3] is the case that needs the tie rule: at the first step nums[mid] == nums[hi], and the minimum lies to the right of mid, so neither hi = mid nor any halving rule would be safe.
Complexity
Section titled “Complexity”- Time: when ties are rare, but in the worst case — for an input like
[1,1,1,1,0,1,1]almost every step is a tie that removes only one element. - Space: .
Follow-up answer: yes, duplicates change the worst case from to . When nums[mid] == nums[hi] (and possibly nums[lo] too), the information we see is identical whether the minimum is on the left or the right, so no algorithm can discard half the range; in the extreme [1,1,...,1,0,1,...,1] any algorithm must inspect up to every element to find the 0.
Edge cases
Section titled “Edge cases”- All elements equal: every step is a tie,
hiwalks down to0, andnums[0]is returned. - Not rotated (Example 1): the
<branch always applies and the search behaves like the unique-element version. - Single element: the loop does not run.
Think before you peek
Work out an approach on your own first — the explanation will still be here when you want it.
class Solution { public: int findMin(vector<int>& nums) { int lo = 0, hi = (int)nums.size() - 1; while (lo < hi) { int mid = lo + (hi - lo) / 2; if (nums[mid] > nums[hi]) lo = mid + 1; else if (nums[mid] < nums[hi]) hi = mid; else hi--; // nums[hi] has a copy at mid, so dropping hi is safe } return nums[lo]; }};class Solution { public int findMin(int[] nums) { int lo = 0, hi = nums.length - 1; while (lo < hi) { int mid = lo + (hi - lo) / 2; if (nums[mid] > nums[hi]) lo = mid + 1; else if (nums[mid] < nums[hi]) hi = mid; else hi--; // nums[hi] has a copy at mid, so dropping hi is safe } return nums[lo]; }}class Solution: def findMin(self, nums: list[int]) -> int: lo, hi = 0, len(nums) - 1 while lo < hi: mid = (lo + hi) // 2 if nums[mid] > nums[hi]: lo = mid + 1 elif nums[mid] < nums[hi]: hi = mid else: hi -= 1 # nums[hi] has a copy at mid, so dropping hi is safe return nums[lo]Give it a try first
Attempt the problem on LeetCode — the code will still be here.
![Binary search with duplicates on [3,3,1,3], step 1 of 4](/CodePrime/img/codes/find-minimum-in-rotated-sorted-array-ii/search/01.webp)
![Binary search with duplicates on [3,3,1,3], step 2 of 4](/CodePrime/img/codes/find-minimum-in-rotated-sorted-array-ii/search/02.webp)
![Binary search with duplicates on [3,3,1,3], step 3 of 4](/CodePrime/img/codes/find-minimum-in-rotated-sorted-array-ii/search/03.webp)
![Binary search with duplicates on [3,3,1,3], step 4 of 4](/CodePrime/img/codes/find-minimum-in-rotated-sorted-array-ii/search/04.webp)