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.