Python binary search solution - O(logN) 36ms


  • 1
    G
    class Solution:
        # @param {integer[]} nums
        # @return {integer}
        def findMin(self, nums):
            if not nums:
                return -1
    
            low, high = 0, len(nums) - 1
            mid = (low + high) / 2
    
            while low < high:
                if nums[mid] > nums[mid + 1]:
                    return nums[mid + 1]
                elif nums[mid] > nums[high]:
                    low = mid + 1
                else:
                    high = mid
                mid = (low + high) / 2
    
            return nums[mid]

Log in to reply
 

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