LeetCode: Find Minimum in Rotated Sorted Array II Posted on February 14, 2018July 26, 2020 by braindenny Find Minimum in Rotated Sorted Array II Similar Problems: LeetCode: Find Minimum in Rotated Sorted Array LeetCode: Search in Rotated Sorted Array CheatSheet: Leetcode For Code Interview CheatSheet: Common Code Problems & Follow-ups Tag: #binarysearch, #rotatelist Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element. The array may contain duplicates. Github: code.dennyzhang.com Credits To: leetcode.com Leave me comments, if you have better ways to solve. // https://code.dennyzhang.com/find-minimum-in-rotated-sorted-array-ii // Basic Ideas: binary search // If nums[mid] > nums[right], it's in the right half, (excluding mid) // If nums[mid] < nums[right], it's in the left half // If nums[mid] = nums[right], drop nums[right] // Complexity: Time O(log(n)), Space O(1) func findMin(nums []int) int { left, right := 0, len(nums)-1 // must exists for left<right { mid := (right-left)/2+left if nums[mid] > nums[right] { left = mid+1 } else { if nums[mid] < nums[right] { // drop the left half right = mid } else { right-- } } } return nums[left] } Post Views: 2