#153

Find Minimum in Rotated Sorted Array

Medium Compare with the right end

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

A sorted array of distinct values was rotated. Return its smallest element in O(log n).

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: the smallest value of a rotated sorted array.

Why this works: if nums[mid] > nums[hi], the "drop" is to the right of mid; otherwise the minimum is at mid or to the left.

Example: [3, 4, 5, 1, 2]

mid 2 (5) > 2 → lo 3mid 3 (1) ≤ 2 → hi 3 → answer 1

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Comparing with nums[lo] — for an unrotated array that sends you the wrong way.

Solve on LeetCode ↗
Complexity
  • Time: O(log n).
  • Space: O(1).
Topic: Binary Search
🎬 Episodes 21–23: video coming soon