⚡Flashcard Drill➔
NeetCode #284LC-153MediumBinary SearchBlind 75NC 150NC 250
← Back to All Problems

#284 · #153 · Find Minimum in Rotated Sorted Array(寻找旋转排序数组中的最小值)

📌 Problem Statement & Constraints

Suppose an array of distinct integers sorted ascending is rotated between 1 and n times. Given the rotated array nums, return its minimum element. Constraints: 1 <= n <= 5000, -5000 <= nums[i] <= 5000, all values distinct. The follow-up asks for O(log n).

💡 Core Algorithmic Approaches

  1. Compare the middle element with the rightmost element. If nums[mid] > nums[right], the minimum lies strictly to the right of mid.
  2. Otherwise the minimum is at mid or to its left, so shrink the right bound to mid.
  3. Using the rightmost element (rather than the leftmost) is what makes the comparison unambiguous in the rotated array.
  4. The loop ends when lo == hi, which is the index of the minimum.

💻 Benchmark Python3 Implementation

class Solution:
    def findMin(self, nums: List[int]) -> int:
        lo, hi = 0, len(nums) - 1
        while lo < hi:
            mid = (lo + hi) // 2
            if nums[mid] > nums[hi]:   # minimum is to the right
                lo = mid + 1
            else:                      # minimum is at mid or to the left
                hi = mid
        return nums[lo]

⚡ Complexity Deep Dive

⏱️ Time Complexity
O(log n): the search space halves each iteration.
💾 Space Complexity
O(1): three indices.

⚠️ Interview Pitfalls & Follow-ups

  • Comparing with nums[lo] instead of nums[hi]: with the leftmost element the comparison does not distinguish the two halves as cleanly, and the boundary conditions become subtle.
  • Using hi = mid - 1: that could skip the minimum; use hi = mid.
  • Returning nums[mid] when the loop ends: the loop ends at lo == hi, which is the minimum index.
  • Assuming the array is rotated at least once: an unrotated (already sorted) array is handled correctly, since the comparison always shrinks the right bound.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🎯 Drill This Card Now➔

🧩 Related Binary Search Problems

中文速览与面试要点

寻找旋转排序数组中的最小值

已知一个长度为 n 的数组,预先按照升序排列,经由 1 到 n 次 旋转 后,得到输入数组。例如,原数组 nums = [0,1,2,4,5,6,7] 在变化后可能得到 [4,5,6,7,0,1,2] 。给你一个元素值 互不相同 的数组 nums,它原来是一个升序排列的数组,并按上述情形进行了多次旋转。请你找出并返回数组中的 最小元素 。你必须设计一个时间复杂度为 O(log n) 的算法解决此问题。约束:n == nums.length;1 ≤ n ≤ 5000;-5000 ≤ nums[i] ≤ 5000;nums 中的所有整数互不相同;nums 原来是一个升序排序的数组,并进行了 1 至 n 次旋转。

核心要点

  • 与右端点比较(标准解) :比较 nums[mid] 与 nums[r] ,而不是与 nums[l] 比较。
  • 若 nums[mid] &gt; nums[r] :说明最小值在 mid 的右侧(mid 处是较大段的元素),因此 l = mid + 1 。
  • 若 nums[mid] &lt; nums[r] :说明 [mid, r] 是递增的,最小值在 mid 或 mid 左侧,因此 r = mid 。
  • 为什么与 nums[r] 比较而非 nums[l] :与 nums[l] 比较时,「左半是否有序」无法直接判定最小值位置(例如 [4,5,6,7,0,1,2] 中 nums[mid] &gt; nums[l] 但最小值在右侧)。与右端点比较则始终能确定方向。

常见陷阱

  • 与 nums[l] 比较 :会得到错误的收缩方向。必须与 nums[r] 比较。
  • r = mid 写成 r = mid − 1 : nums[mid] 本身可能是最小值,不能排除。
  • 循环条件写成 l &lt;= r :本题用 l &lt; r ,结束时 l == r 即为答案位置。用 l &lt;= r 会多一次无意义迭代。
  • 数组未旋转时 :此时 nums[mid] &lt; nums[r] 恒成立,r 会一路收缩到 0,返回 nums[0] ,正确。

参考实现

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             # 最小值在左半(含 mid)
        return nums[l]