Problem overview
The method findMin receives a list of distinct integers, nums, that was sorted in ascending order and then possibly rotated: its first k elements were moved, in the same order, to the end. For example, [0, 1, 2, 4, 5, 6, 7] rotated at k = 3 becomes [4, 5, 6, 7, 0, 1, 2]. The method returns the smallest element of the rotated list.
In [3, 4, 5, 1, 2] the minimum is 1, and in [11, 13, 15, 17], which was not rotated, it is 11, the first element. The lesson asks for O(log n) time and O(1) extra space. Calling min(nums) or scanning the list is O(n), so the sorted structure has to be used.
Approach: compare the middle with the last element
The rotated list rises, drops once at the rotation point, and rises again. The minimum is the element right after the drop, or the first element when there is no rotation. Binary search can locate it if each comparison tells which side of the middle the drop is on.
The comparison that works is nums[middle] against nums[right]:
- If
nums[middle] > nums[right], the values must drop somewhere between them, because in a stretch with no drop every value is smaller than the last one. The minimum is to the right ofmiddle, andmiddleitself can't be the minimum, since it is bigger than another element. - Otherwise
nums[middle] < nums[right](the values are distinct, andmiddleis always left ofrightinside the loop), and everything frommiddletorightis sorted. The minimum is atmiddleor to its left.
Every pass keeps the minimum inside the window and discards about half of it. When the window is down to a single index, that index holds the minimum.
On [4, 5, 6, 7, 0, 1, 2], the window starts at indexes 0 to 6 with middle 3, which holds 7. That is bigger than the last element, 2, so left moves to 4. Then middle is 5, which holds 1; it is smaller than 2, so right moves to 5. With the window at indexes 4 and 5, middle is 4, which holds 0, so right moves to 4. The window is a single index, and nums[4] is 0.
Step by step
- Set the window:
left, right = 0, len(nums) - 1. The minimum can be anywhere, so the window starts as the whole list. - Loop while
left < right. This time the comparison is<, not<=: the loop stops when one index is left, and that index holds the minimum. - Compute
middleasleft + (right - left) // 2, using floor division. - If
nums[middle] > nums[right], the drop is to the right ofmiddle. Movelefttomiddle + 1, skippingmiddle, which can't be the minimum. - Otherwise move
righttomiddle, notmiddle - 1, becausenums[middle]may be the minimum itself. - After the loop,
leftandrighthave met. Returnnums[left].
The solution in Python
class Solution:
def findMin(self, nums: List[int]) -> int:
left, right = 0, len(nums) - 1
while left < right:
middle = left + (right - left) // 2
if nums[middle] > nums[right]:
left = middle + 1
else:
right = middle
return nums[left]
The Python details that matter here:
//is floor division. It gives an integer index, where/would give a float that can't index a list.- The middle rounds down. With two elements left,
middleequalsleft, neverright. That matters for theelsebranch:right = middlealways shrinks the window, so the loop can't get stuck. Ifmiddlecould equalright,right = middlewould change nothing. - The loop condition
left < rightmeans a one-element list skips the loop and returns its only element. - Tuple assignment in
left, right = 0, len(nums) - 1sets two variables in one line.
Complexity
Time: O(log n). Each pass discards about half of the window, so the loop runs at most about log2 n times, with constant work per pass.
Space: O(1). The method keeps left, right and middle, and never copies the list.
Edge cases and common mistakes
- A list that was not rotated.
[11, 13, 15, 17]gives 11. Everynums[middle]is smaller thannums[right], sorightkeeps moving left until it reaches index 0. - One element.
[7]gives 7 without entering the loop. - Two elements.
[2, 1]gives 1 and[1, 2]gives 1. - The minimum is the last element.
[2, 3, 1]gives 1:nums[1]is bigger thannums[2], soleftjumps to the last index and the loop ends. - Comparing with
nums[left]. On[11, 13, 15, 17],nums[middle]is bigger thannums[left]even though nothing is rotated, and a rule that sends the search right on that comparison returns 15 instead of 11. Compare withnums[right]. - Using
right = middle - 1. It can discard the minimum. On[4, 5, 1, 2, 3]that version returns 4 instead of 1. - Using
left = middle. Because the middle rounds down,left = middlecan leave the window unchanged and loop forever. Moving tomiddle + 1is safe becausenums[middle]is known to be too big.
Testing
[3, 4, 5, 1, 2]gives1.[4, 5, 6, 7, 0, 1, 2]gives0.[11, 13, 15, 17]gives11.[7]gives7.[2, 3, 1]gives1.
