Skip to content

153. Find Minimum in Rotated Sorted Array

https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/

js
/**
 * @param {number[]} nums
 * @return {number}
 */
var findMin = function(nums) {
    var l = 0
    var r = nums.length - 1

    while (l < r) {
        let mid = (l + r) / 2 >> 0
        if (nums[mid] < nums[l]) {
            r = mid
        } else if (nums[mid] > nums[r]) {
            l = mid + 1
        } else {
            return Math.min(nums[l], nums[mid])
        }
    }

    return nums[l]
}
py
class Solution(object):

    def findMin(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        l, r = 0, len(nums) - 1
        while l < r:
            mid = (l + r) // 2
            if nums[mid] < nums[l]:
                r = mid
            elif nums[mid] > nums[r]:
                l = mid + 1
            else:
                return min(nums[l], nums[mid])
        return nums[l]