PYTHON binary search, worst case O(n)


  • 0
    4
    class Solution:
        # @param {integer[]} nums
        # @return {integer}
        def findMin(self, nums):
            l = len(nums) - 1
            if l <= 1:
                return min(nums)
            mid = l / 2
            if nums[0] < nums[mid]:
                return self.findMin([nums[0]] + nums[mid+1:])
            elif nums[0] > nums[mid]:
                return self.findMin(nums[1:mid+1])
            else:
                return min(self.findMin(nums[0:mid]),self.findMin(nums[mid+1:]))

Log in to reply
 

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