Why not solve the problem directly by comparing each element with the first?

  • 0
    class Solution(object):
    def findMin(self, nums):
        :type nums: List[int]
        :rtype: int
        if len(nums)==0: return
        current_min = nums[0]
        for num in nums:
            if num<current_min:
                return num
        return current_min

    The answer is simply the first element or the first following element less than the first one. It seems much easier than the solution.

  • 1

    Because that would be O(N) worse time complexity. The optimal solution uses binary search and achieves O(log N) time this way.

Log in to reply

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