Problem overview
The method findMin receives nums, an array of distinct integers that was sorted in ascending order and then possibly rotated: a rotation at index k moves the first k elements, in the same order, to the end. It returns the smallest element. The algorithm must run in O(log n) time with O(1) extra space.
For [4, 5, 6, 7, 0, 1, 2], the original sorted array was [0, 1, 2, 4, 5, 6, 7] rotated at k = 3, and the minimum is 0. For [11, 13, 15, 17] there was no rotation, so the minimum is the first element, 11.
Approach: compare the middle with the last element
Scanning the array, or calling Math.min(...nums), is O(n). A rotated array has a single drop, where a big value is followed by a small one, and the minimum is right after it. Binary search can find that drop without looking at every element.
Look at nums[middle] and nums[right]:
- If
nums[middle] > nums[right], the values must drop somewhere between them, so the minimum lies to the right ofmiddle. Andnums[middle]itself is bigger than another element, so it can't be the minimum. - Otherwise
nums[middle]is smaller thannums[right](the values are distinct), and everything frommiddletorightis sorted. The minimum is atmiddleor to its left.
Each pass keeps the minimum inside the window and cuts the window roughly in half. When the window is a single index, that index holds the minimum.
On [4, 5, 6, 7, 0, 1, 2] the window starts as indices 0 to 6. The middle, index 3, holds 7, which is bigger than the last value 2, so left moves to 4. The window 4 to 6 has its middle at index 5, holding 1, which is not bigger than 2, so right moves to 5. The window 4 to 5 has its middle at index 4, holding 0, so right moves to 4. The window is one index, and nums[4] is 0.
Step by step
- Set the window with one
let:leftis 0 andrightisnums.length - 1. The minimum can be anywhere, so the window starts as the whole array. - Loop while
left < right. The loop stops when the window is down to one index, and that index holds the minimum. This is<, not<=. - Compute
middleasMath.floor(left + (right - left) / 2). Dividing numbers keeps the fraction, and an index must be whole. - If
nums[middle] > nums[right], movelefttomiddle + 1. The minimum is to the right ofmiddle. - Otherwise move
righttomiddle, notmiddle - 1, becausenums[middle]may be the minimum itself. - Return
nums[left]. The two pointers meet at the minimum.
The solution in TypeScript
class Solution {
findMin(nums: number[]): number {
let left = 0, right = nums.length - 1;
while (left < right) {
let middle = Math.floor(left + (right - left) / 2);
if (nums[middle] > nums[right]) {
left = middle + 1;
} else {
right = middle;
}
}
return nums[left];
}
}
Some TypeScript details:
Math.flooris needed. Anumberis a double, so(0 + 7) / 2is 3.5, andnums[3.5]isundefined.Math.floormakes the index whole. Hereleftnever exceedsright, so the result is always a valid index.- Why
left + (right - left) / 2. It picks the lower of two middles when the window has an even length. That matters for theright = middlebranch: with a two-element window,middleequalsleft, so the window always shrinks and the loop ends. - Return value. The function returns a value, not an index, so it ends with
nums[left]. The array is not changed.
Complexity
Time: O(log n). Each pass discards at least half of the window, so there are about log2 n passes with constant work each.
Space: O(1). Only left, right and middle are stored.
Edge cases and common mistakes
- Comparing with
nums[left]instead ofnums[right]. In an array that wasn't rotated,nums[middle]is bigger thannums[left], yet the minimum is on the left. A version that movesleftin that case returns 15 for[11, 13, 15, 17]instead of 11. - Using
right = middle - 1. It can step over the minimum whennums[middle]is the minimum itself. For[2, 0, 1]that version returns 2 instead of 0. - Using
left <= right. Withright = middlein the loop, the window may never shrink whenleftequalsright, so the loop would not end. - One element.
[1]never enters the loop and returns 1. - No rotation. The middle is never bigger than the last value, so
rightkeeps moving left to 0, and the first element is returned. - Duplicates. With repeated values,
nums[middle] == nums[right]would not say which side holds the minimum. The lesson's constraints say all values are distinct.
Testing
[3, 4, 5, 1, 2]gives1.[4, 5, 6, 7, 0, 1, 2]gives0.[11, 13, 15, 17]gives11.[2, 1]gives1.[1]gives1.
