class Solution {
public int findMin(int[] nums) {
int left = 0;
int right = nums.length - 1;
while (left < right) {
int mid = left + (right - left) / 2; // Calculate mid to avoid potential overflow
int midVal = nums[mid];
// Check if mid is the potential minimum
if (midVal < nums[right]) {
// Special case: mid is the minimum and its predecessor is larger
if (mid > 0 && nums[mid - 1] > midVal) {
return midVal;
}
// Mid value is smaller than the rightmost element, so the minimum must be
// in the left half
right = mid - 1;
} else {
// Mid value is greater to the rightmost element, so the minimum
// must be in the right half (larger elements have rotated to the front)
left = mid + 1;
}
}
// If left == right, we've narrowed down to a single element, which is the minimum
return nums[left];
}
}
- 1st attempt (go), recursive binary search → pretty confusing