private: intfindMin(const vector<int>& nums, int lo, int hi) { // only one element if (lo >= hi) return nums[lo];

if (isSorted(nums, lo, hi)) return nums[lo]; int mid = lo + (hi - lo) / 2; int left = findMin(nums, lo, mid); int right = findMin(nums, mid + 1, hi); returnmin(left, right); }

boolisSorted(const vector<int>& nums, int lo, int hi) { return nums[lo] < nums[hi]; }

Time: $O(\log{N})$

False: $T(N) = 2T(N/2) = O(N)$ ❌

At each level, at least one side could be done in $O(1)$.

True: $T(N) = O(1) + T(N/2) = O(\log{N})$

Space: $O(\log{N})$

Binary Search

The basic idea is that if nums[mid] >= nums[lo] we assure that the inflection point is on the right part.

private: intfindMin(const vector<int>& nums, int lo, int hi) { // only one element if (lo == hi) return nums[lo]; // the subarray is sorted if (nums[lo] < nums[hi]) return nums[lo]; int mid = lo + (hi - lo) / 2;

public: intfindMin(vector<int>& nums) { int lo = 0; int hi = nums.size() - 1; // stops when there is only one element // in other words, it makes sure that there are at least 2 elements while (lo < hi) { if (nums[lo] < nums[hi]) return nums[lo]; int mid = lo + (hi - lo) / 2; if (nums[mid] >= nums[lo]) { lo = mid + 1; } else { hi = mid; } } return nums[lo]; }