↓Skip to main content
  1. LeetCode/

LeetCode 153: Find Minimum in Rotated Sorted Array

·2 mins· ·
LeetCode Medium Array Binary-Search
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2022-10-28

  • Submissions: Wrong Answer → Accepted ×2 → Wrong Answer ×6 → Accepted → Wrong Answer ×2 → Accepted ×4

Approach
#

The minimum is the rotation point. Compare nums[mid] with nums[r] (Binary Search):

  • nums[mid] > nums[r]: the break is right of mid, so l = mid + 1.
  • Otherwise [mid, r] is increasing and the minimum is at mid or to its left, so r = mid.

With while l < r, the final l == r is the minimum. Comparing with the right end is safer: comparing with the left end needs a special case for an unrotated array.

My Attempts
#

The first nine WAs compared nums[mid] with the left end using incomplete conditions, failing even a two-element rotation like [5, 4] (returned 5). Comparing with the right end is much simpler.

Solution
#

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

Complexity
#

Time O(log n), space O(1).

Mistakes to Avoid
#

  • r = mid, not mid - 1: mid itself may be the minimum.

Interview Explanation
#

The minimum is the break point: if the middle exceeds the right end the break is to the right, otherwise the minimum is at or left of the middle, so I shrink the right bound to it. Where the bounds meet is the minimum. Time O(log n).

Related

LeetCode 875: Koko Eating Bananas
·2 mins
LeetCode Medium Array Binary-Search
LeetCode 875 note: binary search the speed for the smallest k with total hours <= h, continuing left even when the hours equal h
LeetCode 167: Two Sum II - Input Array Is Sorted
·2 mins
LeetCode Medium Array Two-Pointers Binary-Search
LeetCode 167 note: sorted, so move two pointers inward, shifting right when the sum is too large and left when too small; indices are 1-based
LeetCode 81: Search in Rotated Sorted Array II
·2 mins
LeetCode Medium Array Binary-Search
LeetCode 81 note: with duplicates, nums[l] == nums[mid] hides which half is sorted, so just move l right by one; worst case O(n)
LeetCode 74: Search a 2D Matrix
·2 mins
LeetCode Medium Array Binary-Search Matrix
LeetCode 74 note: each row continues the previous one in sorted order, so binary search it as a 1-D array of length m × n, mapping mid to (mid // n, mid % n)
LeetCode 33: Search in Rotated Sorted Array
·2 mins
LeetCode Medium Array Binary-Search
LeetCode 33 note: after rotation one side of mid is always sorted; check whether the target lies in it and search there, otherwise search the other side
LeetCode 34: Find First and Last Position of Element in Sorted Array
·2 mins
LeetCode Medium Array Binary-Search
LeetCode 34 note: two lower bounds: the first position >= target, and the first position >= target + 1 minus one