Zheaoli / do-something-right

MIT License
37 stars 3 forks source link

2022-09-27 #373

Open Zheaoli opened 2 years ago

Zheaoli commented 2 years ago

2022-09-27

Dapeus commented 2 years ago

image

linupychiang commented 2 years ago
image
gongpeione commented 2 years ago
/*
 * @lc app=leetcode id=153 lang=typescript
 *
 * [153] Find Minimum in Rotated Sorted Array
 */

// @lc code=start
function findMin(nums: number[]): number {
    let min = nums[0];
    let left = 0;
    let right = nums.length - 1;

    while (left <= right) {
        // if left to right is sorted
        // then the left is the minimum value
        if (nums[left] < nums[right]) {
            min = Math.min(nums[left], min);
            break;
        }

        const middle = ~~((left + right) / 2);
        min = Math.min(nums[middle], min);
        // if the middle value is greater or equal to left value
        // then we need to search right portion
        if (nums[middle] >= nums[left]) {
            left = middle + 1;
        } else {
            right = middle - 1;
        }
    }

    return min;
};
// @lc code=end

微信id: 弘树 来自 vscode 插件