Problem overview
The method findMin receives a vector<int> named nums of distinct integers. The vector 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.
For [3, 4, 5, 1, 2] the answer is 1, and for [4, 5, 6, 7, 0, 1, 2] it is 0. For [11, 13, 15, 17], which was not rotated (k = 0), the answer is 11, the first element. The statement asks for O(log n) time and O(1) extra space, with up to a million elements.
Approach: compare the middle with the last element
Scanning the whole vector for the minimum is O(n). Rotation doesn't scramble the vector, though: it rises, drops once at the rotation point, and rises again. The minimum is the element right after the drop, or the first element if there is no drop. Binary search can find that point.
At each step, compare the middle element with the last element of the window:
- If
nums[middle] > nums[right], the values must drop somewhere betweenmiddleandright. A sorted stretch ends with its largest value, so a middle that is bigger than the end can't be in one. The minimum is strictly to the right ofmiddle. - Otherwise
nums[middle] < nums[right](the values are distinct, so they can only be equal whenmiddle == right). Everything frommiddletorightis sorted, so the minimum is atmiddleor to its left.
Each pass halves the window, and the minimum always stays inside it. When the window is a single index, that index holds the minimum.
In [4, 5, 6, 7, 0, 1, 2], the middle element is 7 and the last is 2. Since 7 is bigger than 2, the drop lies after the middle, and so does the minimum.
Step by step
- Declare
leftandrightasints on one line: 0 andnums.size() - 1. The minimum can be anywhere, so the window is the whole vector. - Loop while
left < right. This time the test is<, not<=: the loop stops when the window is one index, and that index is the answer. - Compute
middleasleft + (right - left) / 2. Integer division rounds down, somiddleis always less thanrightwhile the window has at least two elements. - If
nums[middle] > nums[right], setleft = middle + 1. The middle element is bigger than another element, so it can't be the minimum, and it can be skipped. - Otherwise set
right = middle. Notmiddle - 1:nums[middle]may itself be the minimum. - After the loop, return
nums[left].leftandrighthave met.
The solution in C++
class Solution {
public:
int findMin(vector<int> &nums) {
int left = 0, right = nums.size() - 1;
while (left < right) {
int middle = left + (right - left) / 2;
if (nums[middle] > nums[right]) {
left = middle + 1;
} else {
right = middle;
}
}
return nums[left];
}
};
The C++ details worth knowing:
- The loop must make progress. Because
middleis rounded down, it is always less thanrightwhenleft < right. So both updates shrink the window:left = middle + 1movesleftup, andright = middlemovesrightdown. If the midpoint rounded up,right = middlecould repeat forever. - Signed indices.
nums.size()is unsigned, butrightis anintholdingnums.size() - 1. For a non-empty vector that is the last index. The constraints guarantee at least one element. - One declaration, two variables.
int left = 0, right = nums.size() - 1;declares twoints with their own initial values. - The vector is passed by reference, so the up-to-a-million elements are not copied, and the function only reads them.
Tracing the example
With nums = [4, 5, 6, 7, 0, 1, 2], the windows are:
| left | right | middle | nums[middle] | nums[right] | Action |
|---|---|---|---|---|---|
| 0 | 6 | 3 | 7 | 2 | 7 is bigger, so left = 4 |
| 4 | 6 | 5 | 1 | 2 | 1 is smaller, so right = 5 |
| 4 | 5 | 4 | 0 | 1 | 0 is smaller, so right = 4 |
Now left and right are both 4, the loop ends, and the function returns nums[4], which is 0.
Complexity
Time is O(log n). Each pass at least halves the window, and does one comparison.
Extra space is O(1). Three integers, no recursion.
Edge cases and common mistakes
- Not rotated.
[11, 13, 15, 17]returns 11. The middle is never bigger than the last element, sorightkeeps moving tomiddleuntil it reaches index 0. - One element.
[1]returns 1 without entering the loop. - Two elements.
[2, 1]returns 1 and[1, 2]returns 1. In[2, 1]the middle is index 0, the value 2 is bigger than the last element 1, soleftbecomes 1. - Comparing with
nums[left]instead ofnums[right]. In an unrotated vector the middle is always bigger than the first element, yet the minimum is on the left. A version that compares withnums[left]returns 15 for[11, 13, 15, 17]and 5 for[2, 3, 4, 5, 1], both wrong. - Writing
right = middle - 1. This can discard the minimum when it sits atmiddle. For[3, 1, 2], the middle is 1, which is the minimum, and the version withmiddle - 1returns 3. - Using
left <= right. Withright = middleas an update, this condition can loop forever when the window is one element, becauserightstops changing. The loop must useleft < right. - Duplicates. The values are distinct here. With repeated values,
nums[middle] == nums[right]no longer tells which side holds the minimum, and the search needs extra handling.
