Description
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 rotated4
times.[0,1,4,4,5,6,7]
if it was rotated7
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 overall operation steps as much as possible.
Examples:
Example 1:
Input: nums = [1,3,5]
Output: 1
Example 2:
Input: nums = [2,2,2,0,1]
Output: 0
Solution in Python
from typing import List
class Solution:
def findMin(self, nums: List[int]) -> int:
# Initialize two pointers for the binary search
left, right = 0, len(nums) - 1
while left < right:
# Calculate the mid index
mid = (left + right) // 2
# If the middle element is greater than the rightmost element,
# the minimum must be in the right half (excluding mid)
if nums[mid] > nums[right]:
left = mid + 1
# If the middle element is less than the rightmost element,
# the minimum must be in the left half (including mid)
elif nums[mid] < nums[right]:
right = mid
# If the middle element is equal to the rightmost element,
# we cannot determine the exact position of the minimum,
# but we can safely move the right pointer left by one
else:
right -= 1
# When left meets right, we've found the minimum element
return nums[left]
Explanation:
- Initialization:
- Two pointers
left
andright
are initialized to point to the start and end of the array, respectively.
- Two pointers
- Binary Search Loop:
- The loop continues until
left
is equal toright
. mid
is calculated as the midpoint of the currentleft
andright
pointers.
- The loop continues until
- Comparison and Pointer Adjustment:
- If
nums[mid]
is greater thannums[right]
, it indicates that the smallest value is in the right half of the array (excludingmid
), soleft
is moved tomid + 1
. - If
nums[mid]
is less thannums[right]
, it indicates that the smallest value is in the left half of the array (includingmid
), soright
is moved tomid
. - If
nums[mid]
is equal tonums[right]
, the exact position of the minimum is uncertain, but we can safely decrementright
by one to narrow the search space.
- If
- Result:
- When the loop exits,
left
will be pointing to the minimum element in the array.
- When the loop exits,
This approach ensures that the number of operations is minimized while handling the presence of duplicates effectively. The worst-case time complexity is O(n) when duplicates are present, but it performs significantly better on average due to the binary search approach.
Solution in Javascript
/**
* @param {number[]} nums
* @return {number}
*/
var findMin = function(nums) {
// Initialize two pointers for the binary search
let left = 0, right = nums.length - 1;
while (left < right) {
// Calculate the mid index
let mid = Math.floor((left + right) / 2);
// If the middle element is greater than the rightmost element,
// the minimum must be in the right half (excluding mid)
if (nums[mid] > nums[right]) {
left = mid + 1;
// If the middle element is less than the rightmost element,
// the minimum must be in the left half (including mid)
} else if (nums[mid] < nums[right]) {
right = mid;
// If the middle element is equal to the rightmost element,
// we cannot determine the exact position of the minimum,
// but we can safely move the right pointer left by one
} else {
right--;
}
}
// When left meets right, we've found the minimum element
return nums[left];
};
Solution in Java
class Solution {
public int findMin(int[] nums) {
// Initialize two pointers for the binary search
int left = 0, right = nums.length - 1;
while (left < right) {
// Calculate the mid index
int mid = left + (right - left) / 2;
// If the middle element is greater than the rightmost element,
// the minimum must be in the right half (excluding mid)
if (nums[mid] > nums[right]) {
left = mid + 1;
// If the middle element is less than the rightmost element,
// the minimum must be in the left half (including mid)
} else if (nums[mid] < nums[right]) {
right = mid;
// If the middle element is equal to the rightmost element,
// we cannot determine the exact position of the minimum,
// but we can safely move the right pointer left by one
} else {
right--;
}
}
// When left meets right, we've found the minimum element
return nums[left];
}
}
Solution in C#
public class Solution {
public int FindMin(int[] nums) {
// Initialize two pointers for the binary search
int left = 0, right = nums.Length - 1;
while (left < right) {
// Calculate the mid index
int mid = left + (right - left) / 2;
// If the middle element is greater than the rightmost element,
// the minimum must be in the right half (excluding mid)
if (nums[mid] > nums[right]) {
left = mid + 1;
// If the middle element is less than the rightmost element,
// the minimum must be in the left half (including mid)
} else if (nums[mid] < nums[right]) {
right = mid;
// If the middle element is equal to the rightmost element,
// we cannot determine the exact position of the minimum,
// but we can safely move the right pointer left by one
} else {
right--;
}
}
// When left meets right, we've found the minimum element
return nums[left];
}
}