We don't need binary search for this problem, do we?


  • 0
    W
    class Solution(object):
    
        def findMin(self, nums):
            if nums[0] <= nums[-1]:
                return nums[0]
            
            for i in range(len(nums)-1):
                if nums[i+1] < nums[i]:
                    return nums[i+1]
    

  • 0

    @wonderfuly Using Binary Search the time complexity will be O(logn) while your solution will give us O(n). Just imagine this case where you need to frequently retrieve the minimum in a set of large-scale different arrays. The difference will be huge. You should try to optimise it instead of just working it out, since you're here to improve yourself, right?


  • 0
    W

    @LHearen you're right, thanks!


Log in to reply
 

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