Problem overview
The function findMin receives a slice 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. It returns the smallest element. For example, [0, 1, 2, 4, 5, 6, 7] rotated at k = 3 becomes [4, 5, 6, 7, 0, 1, 2], and the answer is 0. With k = 0 the slice is unchanged, so [11, 13, 15, 17] gives 11.
The budget is O(log n) time and O(1) extra space, so scanning the whole slice for the minimum is too slow, and the sorted structure has to be used.
Approach: compare the middle with the last element
The minimum sits at the rotation point, the one place where the values drop. In [4, 5, 6, 7, 0, 1, 2] it is the 0 right after the 7. Binary search can find that point if each comparison tells which side of middle the drop is on.
Compare nums[middle] with nums[right], the last element of the current window:
- If
nums[middle] > nums[right], the values drop somewhere aftermiddle, since in a sorted stretch every element is smaller than the last one. The minimum is to the right ofmiddle, andmiddleitself can't be it: skip it withleft = middle + 1. - Otherwise
nums[middle]is smaller thannums[right], and everything frommiddletorightis sorted. The minimum is atmiddleor to its left, so the window shrinks toright = middle. It isn'tmiddle - 1, becausenums[middle]may be the minimum itself.
The loop runs while left < right, and ends when the window is one index wide. That index holds the minimum.
The trace for [4, 5, 6, 7, 0, 1, 2]:
left 0, right 6, middle 3: 7 > 2, so left = 4
left 4, right 6, middle 5: 1 > 2 is false, so right = 5
left 4, right 5, middle 4: 0 > 1 is false, so right = 4
left = right = 4: the minimum is nums[4] = 0
Why compare with nums[right] and not nums[left]? With [11, 13, 15, 17], nums[middle] is bigger than nums[left], just as in a rotated slice, yet the minimum is on the left. The last element has no such ambiguity.
Step by step
- Declare both ends in one statement:
leftis 0 andrightislen(nums) - 1. The minimum can be at any index, so the window starts as the whole slice. - Loop while
left < right. Unlike a search for a target, this loop stops at one index, and that index is the answer. - Compute
middleasleft + (right-left)/2, which rounds down. - If
nums[middle] > nums[right], the minimum is to the right ofmiddle: setlefttomiddle + 1. - Otherwise, everything from
middletorightis sorted, so the minimum is atmiddleor to its left: setrighttomiddle. - Return
nums[left]. Each pass kept the minimum inside the window, and the window is now a single index.
The solution in Go
func findMin(nums []int) int {
left, right := 0, len(nums)-1
for left < right {
middle := left + (right-left)/2
if nums[middle] > nums[right] {
left = middle + 1
} else {
right = middle
}
}
return nums[left]
}
The Go details worth a closer look:
- Go's while loop.
for left < right { ... }is aforwith only a condition. Go has nowhilekeyword. - Multiple declaration.
left, right := 0, len(nums)-1declares both variables in one statement. - Rounding down matters here. Because
(right-left)/2rounds down,middleis always less thanrightwhileleft < right. That guaranteesright = middleshrinks the window. Ifmiddlecould equalright, the loop could stay on the same window forever. - The function returns a value, not an index.
nums[left]is the element itself. - An empty slice panics.
len(nums)-1would be -1 andnums[left]would be out of range. The constraints guarantee at least one element.
Complexity
Time: O(log n). Each pass discards at least half of the window: left = middle + 1 drops the left half and the middle, and right = middle drops the right half. So about log₂ n passes run.
Space: O(1). Only left, right and middle are kept, and the slice is not changed.
Edge cases and common mistakes
- One element.
[1]gives 1. The loop condition is false at once, andnums[0]is returned. - Not rotated.
[11, 13, 15, 17]gives 11. The middle is never bigger than the last element, sorightkeeps moving left until it reaches index 0. - Two elements.
[2, 1]gives 1 and[1, 2]gives 1. The middle is index 0, which is compared withnums[1]. - Minimum at the last index.
[2, 3, 4, 5, 1]gives 1. The window keeps moving right until it reaches the last index. - Setting
right = middle - 1. This can skip the minimum whennums[middle]is the minimum itself. For[4, 5, 1, 2, 3]the middle is the 1 on the first pass, and that version returns 4. - Comparing with
nums[left]. A version that movesleftup whenevernums[middle] > nums[left]returns 5 for[5, 1, 2, 3, 4]and 15 for[11, 13, 15, 17], both wrong. - Duplicates. The lesson guarantees distinct values. With repeated values,
nums[middle]equal tonums[right]doesn't say which side holds the minimum.
Testing
[3, 4, 5, 1, 2]gives 1.[4, 5, 6, 7, 0, 1, 2]gives 0.[11, 13, 15, 17]gives 11.[1]gives 1.[2, 1]gives 1.[5, 1, 2, 3, 4]gives 1.[2, 3, 4, 5, 1]gives 1.
