Find Minimum in Rotated Sorted Array

description

Table of Contents

my solutions

class Solution:
    def findMin(self, nums: List[int]) -> int:
        res = min(nums)
        return res
class Solution:
    def findMin(self, nums: List[int]) -> int:
        res = float("inf")
        for i in range(len(nums)):
            if nums[0] < res:
                res = nums[0]
            temp = nums[-1]
            if temp > nums[0]:
                return nums[0]
            nums = nums[0:len(nums)-1]
            nums.insert(0, temp)
            # print(nums)
        return int(res)

binary search solution

class Solution:
    def findMin(self, nums: List[int]) -> int:
        res = nums[0]
        l, r = 0, len(nums) - 1

        while l <= r:
            if nums[l] < nums[r]:
                res = min(res, nums[l])
            
            m = r - l // 2
            res = min(res, nums[m])
            if nums[m] >= nums[l]:
                l = m + 1
            else:
                r = m - 1
        return res