154. Find Minimum in Rotated Sorted Array II
HardSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to find the minimum element in a sorted array that has been rotated. The key challenge is that the array can contain duplicate elements. This is a modification of the "Find Minimum in Rotated Sorted Array" problem, and the presence of duplicates affects the algorithm's worst-case performance.
Solution
Find Minimum in Rotated Sorted Array II in Python
class Solution:
def findMin(self, nums: List[int]) -> int:
left, right = 0, len(nums) - 1
# Loop until the search space is narrowed to one element.
while left < right:
mid = left + (right - left) // 2
if nums[mid] < nums[right]:
# The right part [mid..right] is sorted.
# The minimum must be in the left part [left..mid].
right = mid
elif nums[mid] > nums[right]:
# The pivot (minimum) is in the right part [mid+1..right].
left = mid + 1
else: # nums[mid] == nums[right]
# We can't decide which part has the minimum.
# For example: [3, 1, 3] or [1, 3, 1]. mid is at index 1.
# In this case, we can safely discard nums[right] and check again.
# This is safe because if nums[right] is the minimum, another
# copy of it must exist to its left (possibly nums[mid]).
right -= 1
# At the end of the loop, left == right, and it points to the minimum element.
return nums[left]
Find Minimum in Rotated Sorted Array II in Java
class Solution {
public int findMin(int[] nums) {
int left = 0;
int right = nums.length - 1;
// Loop until the search space is reduced to a single element.
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] < nums[right]) {
// The minimum element must be in the left half, including mid.
// This is because the right half [mid...right] is sorted.
right = mid;
} else if (nums[mid] > nums[right]) {
// The minimum element must be in the right half, excluding mid.
// The pivot point is somewhere to the right of mid.
left = mid + 1;
} else { // nums[mid] == nums[right]
// We cannot determine which half contains the minimum.
// However, we can safely discard the element at 'right'.
// If nums[right] were the minimum, its duplicate nums[mid] would still be in play,
// or another instance of the minimum value must exist in the remaining range.
// In the worst case, this degrades the algorithm to a linear scan.
right--;
}
}
// The loop ends when left and right converge to the minimum element's index.
return nums[left];
}
}
Find Minimum in Rotated Sorted Array II in C++
class Solution {
public:
int findMin(vector<int>& nums) {
int left = 0;
int right = nums.size() - 1;
// Loop until the search space is one element.
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] < nums[right]) {
// The minimum is in the left part or is mid itself.
// The subarray [mid, right] is sorted.
right = mid;
} else if (nums[mid] > nums[right]) {
// The minimum is in the right part.
// The subarray [left, mid] is sorted, but the pivot is on the right.
left = mid + 1;
} else { // nums[mid] == nums[right]
// We can't determine which half has the minimum.
// For example: [3, 1, 3, 3] or [3, 3, 1, 3]
// In this case, we can safely discard nums[right] and check again.
// This is the part that can degrade performance to O(n).
right--;
}
}
// The loop terminates when left == right, which points to the minimum element.
return nums[left];
}
};
Find Minimum in Rotated Sorted Array II in TypeScript
function findMin(nums: number[]): number {
let left = 0;
let right = nums.length - 1;
// Loop until the pointers converge. `left` will eventually point to the minimum.
while (left < right) {
const mid = Math.floor(left + (right - left) / 2);
if (nums[mid] < nums[right]) {
// The subarray from mid to right is sorted.
// The minimum must be in the left half, including mid.
right = mid;
} else if (nums[mid] > nums[right]) {
// The pivot/minimum is in the right half.
// The left half is sorted but contains larger values.
left = mid + 1;
} else { // nums[mid] === nums[right]
// This is the ambiguous case due to duplicates.
// We are not sure about the position of the minimum.
// But we can safely decrement `right` to shrink the search space.
// Example: [3, 1, 3, 3] vs [3, 3, 1, 3].
// Discarding nums[right] won't lose the minimum element from consideration.
right--;
}
}
// The loop terminates when left === right, which is the index of the minimum element.
return nums[left];
};
More Hard LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.