Find Minimum In Rotated Sorted Array
MediumNeetCode 150Top Interview 150 Binary SearchProblem
A list of distinct numbers was sorted in increasing order, then rotated: some number of items from the front were moved to the back, keeping their order.
Given the rotated list nums, return its smallest value. Your solution must run in
O(log n) time.
Constraints
- 1 ≤ nums.length ≤ 5000
- All values in nums are distinct
- nums is a sorted list rotated between 0 and n times
Examples: pick one to animate
Solutions
TimeO(log n)
SpaceO(1)
Time: Every step cuts the range [lo, hi] roughly in half, so about log2(n) steps.
Space: Only the three integers lo, hi and mid are kept.
Compare nums[mid] with nums[hi]. If mid is bigger, the drop (and the minimum) lies right of
mid, so lo = mid + 1. Otherwise the minimum is at mid or left of it, so hi = mid.