LeetCode 153: Find Minimum In Rotated Sorted Array — Step-by-Step Visual Trace


Medium — Binary Search | Array | Divide and Conquer

The Problem

Find the minimum element in a rotated sorted array where all elements are unique. The array was originally sorted in ascending order but rotated at some pivot point.

Approach

Use binary search to efficiently locate the minimum element. Compare the middle element with the rightmost element to determine which half contains the rotation point and minimum value, then search that half.

Time: O(log n) · Space: O(1)

Code

class Solution:
    def findMin(self, nums: List[int]) -> int:
        left, right = 0, len(nums) - 1

        while left < right:
            mid = left + (right - left) // 2

            if nums[mid] > nums[right]:
                left = mid + 1
            else:
                right = mid

        return nums[left]

Watch It Run

Try it yourself: Open TraceLit and step through every line.


Built with TraceLit — the visual algorithm tracer for LeetCode practice.


Comments