Created
May 15, 2026 21:26
-
-
Save tatsuyax25/bd1e6872444c0811270414082bf7aae3 to your computer and use it in GitHub Desktop.
Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become: [4,5,6,7,0,1,2] if it was rotated 4 times.
[0,1,2,4,5,6,7] if it was rotated 7 times.
Notice that ro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number[]} nums | |
| * @return {number} | |
| */ | |
| var findMin = function(nums) { | |
| let left = 0; | |
| let right = nums.length - 1; | |
| while (left < right) { | |
| const mid = Math.floor((left + right) / 2); | |
| // If mid element is greater than the rightmost, | |
| // the minimum is in the right half. | |
| if (nums[mid] > nums[right]) { | |
| left = mid + 1; | |
| } else { | |
| // Otherwise, the minimum is in the left half (including mid) | |
| right = mid; | |
| } | |
| } | |
| return nums[left]; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment