7/26/2020

[LeetCode] 154. Find Minimum in Rotated Sorted Array II

Problem : https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/

Time Complexity = O( Log N )

class Solution:
    def findMin(self, nums: List[int]) -> int:
        left, right = 0, len(nums) - 1
        
        while left < right:
            mid = left + (right - left) // 2
            
            if nums[mid] > nums[right]:
                left = mid + 1
            elif nums[mid] < nums[right]:
                right = mid
            elif nums[mid] == nums[right]:
                while right > mid and nums[mid] == nums[right]:
                    right -= 1
        
        return nums[right]

No comments:

Post a Comment