Find Minimum in Rotated Sorted Array
Find the smallest number in a sorted array that was rotated
DSA, Medium, Binary Search
Which structure or algorithm, and what does it cost.
Take a list of different whole numbers in increasing order. Move some items from its front to its back, keeping their order. For example, [3, 6, 9, 14] can become [9, 14, 3, 6]. (This only shows the move; the examples are below.) Moving nothing is also allowed. You get the rotated list nums. Return the smallest number in it.
Examples
Constraints
- 1 <= length of
nums<= 100,000. - All values are different. Each value is between -1,000,000,000 and 1,000,000,000.
numsis an increasing list with some items moved from the front to the back, possibly none.- Many such lists arrive, each up to 100,000 items, so scanning every item is not accepted.
Your answer must contain
- The simple method and why it is too slow; the better method; code; time and memory cost; a hand trace of example 1.
Target Aim for O(log n) time and O(1) memory.
Hints
Try the problem first. Open one hint at a time.
Hint 1 a nudge
Look at the last item. Which items in the list are larger than it, and which are smaller or equal?
Hint 2 the approach
The smallest item is where the order drops. That point splits the list into two sorted parts. Which part does a middle item belong to?
Hint 3 the key step
Compare the middle with the last item of the range. One result puts the drop on one side. On the other, can the middle itself be the smallest?
Walkthrough
Full walkthrough brute force to best
Brute force
Scan every item and keep the smallest. That is O(n) time and O(1) memory. It works, but it ignores the sorted order, and with 100,000 items every query reads every item.
Best method
Compare the middle item with the last item of the current range. If the middle is larger, the drop from large to small lies to the right of the middle, so the smallest is right of it: move lo to mid + 1. Otherwise the part from the middle to the end is sorted, so the smallest is at the middle or to its left: move hi to mid and keep the middle, because it may be the answer. Repeat while lo < hi. When one item remains, it is the smallest. This also works when nothing was moved: then no middle is larger than the last item, and the range shrinks to the first item.
Hand trace
Run on [14, 18, 23, 3, 6, 9]. Start with lo 0 and hi 5. The middle is position 2, value 23, which is larger than the last item 9, so lo becomes 3. The middle is position 4, value 6, which is not larger than 9, so hi becomes 4. The middle is position 3, value 3, which is not larger than 6, so hi becomes 3. Now lo and hi meet at position 3, so the answer is 3.
Code
def find_min(nums):
lo, hi = 0, len(nums) - 1
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] > nums[hi]:
lo = mid + 1 # the drop is to the right of mid
else:
hi = mid # mid may be the smallest: keep it
return nums[lo]Time and memory
O(log n) time, since each step halves the range: about 17 steps for 100,000 items. O(1) memory.
Common mistakes
- Moving
hitomid - 1when the middle is not larger than the last item. The middle can be the smallest item, so it must stay in the range. - Comparing the middle with the first item of the whole list. On a list that was not moved, such as [5, 8, 12, 20], this sends the search the wrong way.
- Looping while
lo <= hitogether withhi = mid. Whenloequalshithe range stops shrinking and the loop never ends. Uselo < hi.