Skip to content

Prepare For Coder Interview – Denny

  • Basic
  • Medium
  • Hard
  • Architect
  • Life

LeetCode: Find Minimum in Rotated Sorted Array

Posted on January 11, 2018July 26, 2020 by braindenny

Find Minimum in Rotated Sorted Array



Similar Problems:

  • LeetCode: Find Minimum in Rotated Sorted Array II
  • 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.

You may assume no duplicate exists in the array.

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
// Basic Ideas: binary search
//  If nums[mid] > nums[right], the starting point is in the right half
//  Otherwise, it's in the left half
// 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 {
            right = mid
        }
    }
    return nums[left]
}
linkedin
github
slack

Post Views: 5
Posted in AmusingTagged binarysearch, rotatelist

Post navigation

LeetCode: Guess Number Higher or Lower II
LeetCode: Implement Queue using Stacks

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.