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.