Find Minimum in Rotated Sorted Array II - Complete Solution Guide
Find Minimum in Rotated Sorted Array II is LeetCode problem 154, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 rotated 4 times. [0,1,4,4,5,6,7] if it was rotated 7 times. 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 ov
Detailed Explanation
The problem asks us to find the minimum element in a rotated sorted array that may contain duplicate elements. A rotated sorted array is an array that was initially sorted in ascending order but has been shifted a certain number of positions to the right. For example, `[0, 1, 2, 4, 5, 6, 7]` might become `[4, 5, 6, 7, 0, 1, 2]`. The key challenge here is the presence of duplicates, which can make it difficult to determine which half of the array contains the minimum element using a simple binary search.
Solution Approach
The solution uses a modified binary search approach. We maintain two pointers, `left` and `right`, initially pointing to the start and end of the array, respectively. In each iteration, we calculate the middle index `mid`. We compare `nums[mid]` with `nums[right]` to determine which half potentially contains the minimum element. If `nums[mid] > nums[right]`, it means the minimum element is in the right half (from `mid + 1` to `right`). If `nums[mid] < nums[right]`, the minimum element is in the left half (from `left` to `mid`, inclusive). If `nums[mid] == nums[right]`, we decrement `right` by 1, effectively discarding the rightmost element. This step is crucial for handling duplicates and ensuring the algorithm converges.
Step-by-Step Algorithm
- Step 1: Initialize `left` to 0 and `right` to `nums.length - 1`.
- Step 2: While `left < right`:
- Step 3: Calculate `mid` as `left + (right - left) // 2` to prevent potential integer overflow.
- Step 4: If `nums[mid] > nums[right]`, then `left = mid + 1` (minimum is in the right half).
- Step 5: Else if `nums[mid] < nums[right]`, then `right = mid` (minimum is in the left half, including mid).
- Step 6: Else (if `nums[mid] == nums[right]`), then `right--` (discard the rightmost element).
- Step 7: After the loop finishes, `left` and `right` will be equal, pointing to the minimum element. Return `nums[left]`.
Key Insights
- Insight 1: The minimum element will always be the one that is smaller than its preceding element. If the array is not rotated, the first element is the minimum.
- Insight 2: Binary search can be adapted to efficiently search through the rotated array. The standard binary search needs to be modified to handle the rotated and duplicate array conditions.
- Insight 3: Duplicates can hinder the effectiveness of binary search. If `nums[mid] == nums[right]`, it becomes impossible to determine which half contains the minimum using a simple comparison. In this case, we can safely discard `nums[right]` because if `nums[right]` were the minimum, then `nums[mid]` would have to be strictly less than it, which contradicts `nums[mid] == nums[right]`.
Complexity Analysis
Time Complexity: O(log n)
Space Complexity: O(1)
Topics
This problem involves: Array, Binary Search.
Companies
Asked at: Google.