Tensorum
YouTubeనేర్చుకోవడం ప్రారంభించండి
LeetCode 153 · Rotated Sorted Array

Find Minimum in Rotated Sorted Array

LeetCode 153 · మధ్యస్థం · పరిష్కారం, intuition & దశలవారీ వాక్‌త్రూ

nums[mid] < nums[high] ⇒ high = mid, else low = mid + 1.

మధ్యస్థంArrayBinary Search
LeetCode లో తెరవండి ↗

నేను Find Minimum in Rotated Sorted Array (LeetCode 153) ను ఇలా పరిష్కరించాను — Rotated Sorted Array ప్యాటర్న్‌లోని ఒక మధ్యస్థం ప్రాబ్లమ్. కింద: intuition, దశలవారీ approach, నా accepted Python కోడ్, animated test cases, line-by-line debugger.

కింది వివరణ ఇంగ్లీష్‌లో ఉంది.

Intuition

Compare mid with the right end. If nums[mid] < nums[high], the right half is sorted and the minimum is at mid or to its left; otherwise the drop — and the minimum — is to the right of mid.

Approach

  1. 1

    low < high loop.

  2. 2

    Right half sorted → high = mid (mid may be the min).

  3. 3

    Otherwise low = mid + 1.

  4. 4

    Return nums[low].

నేను ఎలా పరిష్కరించాను

Binary search on the dropPythonO(log n) time
class Solution:
    def findMin(self, nums: List[int]) -> int:
        #find mini in which half
        low = 0
        high = len(nums)-1
        while low<high:
            mid = (low+high)//2
            if nums[mid]<nums[high]:#remove the right half
                high = mid
            else:
                low = mid+1 #remove the left half
        return nums[low]
స్టెప్-త్రూ లోడ్ అవుతోంది…

Find Minimum in Rotated Sorted Array — తరచూ అడిగే ప్రశ్నలు

Find Minimum in Rotated Sorted Array వెనుక ఉన్న intuition ఏమిటి?

Compare mid with the right end. If nums[mid] < nums[high], the right half is sorted and the minimum is at mid or to its left; otherwise the drop — and the minimum — is to the right of mid.

Find Minimum in Rotated Sorted Array (LeetCode 153) ను ఎలా పరిష్కరించాలి?

1. low < high loop. 2. Right half sorted → high = mid (mid may be the min). 3. Otherwise low = mid + 1. 4. Return nums[low].

Find Minimum in Rotated Sorted Array యొక్క time, space complexity ఎంత?

Time: O(log n). Space: O(1). (Accepted approach: Binary search on the drop.)

Find Minimum in Rotated Sorted Array లో సాధారణ పొరపాట్లు ఏమిటి?

Compare with high, not low — comparing with low fails on an unrotated array.