153. Find Minimum in Rotated Sorted ArrayΒΆ
The array is sorted in ascending order and then rotated, which means it consists of two sorted parts and the minimum element is the point where the rotation happens. A linear scan would work, but the sorted structure allows a more efficient binary search approach.
With binary search, comparing \(nums[mid]\) with \(nums[right]\) tells which side contains the minimum.
- If \(nums[mid] \lt nums[right]\), the right half from \(mid\) to \(right\) is sorted, so the minimum must be at \(mid\) or to its left.
- Otherwise, the minimum lies strictly to the right of \(mid\)
This process shrinks the search space while always keeping the minimum within the current bounds. When the loop ends, \(left\) points to the minimum element.
Runtime Complexity
Time: \(O(logn)\)
Space: \(O(1)\)