Skip to content

Prepare For Coder Interview – Denny

  • Basic
  • Medium
  • Hard
  • Architect
  • Life

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]
}
linkedin
github
slack

Post Views: 2
Posted in MediumTagged #inspiring, binarysearch, rotatelist

Post navigation

LeetCode: Interleaving String
LeetCode: Paint House

Leave a Reply Cancel reply

Your email address will not be published.

Tags

#array #backtracking #bfs #binarytree #bitmanipulation #blog #classic #codetemplate #combination #dfs #dynamicprogramming #game #graph #greedy #heap #inspiring #interval #linkedlist #manydetails #math #palindrome #recursive #slidingwindow #stack #string #subarray #trie #twopointer #twosum binarysearch editdistance hashmap intervaldp knapsack monotone oodesign presum rectangle redo review rotatelist series sql treetraversal unionfind

Recent Posts

  • a
  • a
  • a
  • a
  • a

Recent Comments

    Archives

    Categories

    • Amusing
    • Basic
    • Easy
    • Hard
    • Life
    • Medium
    • Resource
    • Review
    • Series
    • Uncategorized
    Proudly powered by WordPress | Theme: petals by Aurorum.