Python 95% with binary search and recursion


  • 0
    W

    If nums is rotated, it falls either in "nums[mid] > nums[0] and nums[mid] > nums[-1]" branch or falls in "nums[mid] < nums[0] and nums[mid] < nums[-1]" branch. Otherwise it means nums is sorted and nums[0] is the answer.

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

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.