Tensorum
YouTubeಕಲಿಯಲು ಪ್ರಾರಂಭಿಸಿ
LeetCode 154 · Rotated Sorted Array

Find Minimum in Rotated Sorted Array II

LeetCode 154 · ಕಠಿಣ · ಪರಿಹಾರ, intuition ಮತ್ತು ಹಂತ ಹಂತದ ವಾಕ್‌ಥ್ರೂ

Equal ⇒ high −= 1.

ಕಠಿಣArrayBinary Search
LeetCode ನಲ್ಲಿ ತೆರೆಯಿರಿ ↗

ನಾನು Find Minimum in Rotated Sorted Array II (LeetCode 154) ಅನ್ನು ಹೀಗೆ ಪರಿಹರಿಸಿದೆ — Rotated Sorted Array ಪ್ಯಾಟರ್ನ್‌ನ ಒಂದು ಕಠಿಣ ಪ್ರಾಬ್ಲಮ್. ಕೆಳಗೆ: intuition, ಹಂತ ಹಂತದ approach, ನನ್ನ accepted Python ಕೋಡ್, animated test cases ಮತ್ತು line-by-line debugger.

ಕೆಳಗಿನ ವಿವರಣೆ ಇಂಗ್ಲಿಷ್‌ನಲ್ಲಿದೆ.

Intuition

Same comparison with the right end, plus one case for duplicates: when nums[mid] equals nums[high] you can't tell which side holds the minimum, but you can safely drop high because mid has the same value.

Approach

  1. 1

    nums[mid] > nums[high] → minimum is right: low = mid + 1.

  2. 2

    nums[mid] < nums[high] → high = mid.

  3. 3

    Equal → high −= 1.

  4. 4

    Return nums[low].

ನಾನು ಹೇಗೆ ಪರಿಹರಿಸಿದೆ

Binary search with duplicatesPythonO(log n) avg
class Solution:
    def findMin(self, nums: List[int]) -> int:
        low = 0
        high= len(nums)-1
        while low<high:
            mid = (low+high)//2
            #2,2,2,0,1 if nums[mid]>nums[high],smallest will be on the right half
            if nums[mid]>nums[high]:
                low = mid+1
            #1,3,5 if nums[mid]<nums[high], the lowest must be on the left half, include mid 3,1,3 to handle this special case to include mid as well
            elif nums[mid]<nums[high]:
                high = mid
            #10,1,10,10 reduce the search space if nums[mid] and nums[high] are equal
            else:
                high = high-1
        return nums[low]
ಸ್ಟೆಪ್-ಥ್ರೂ ಲೋಡ್ ಆಗುತ್ತಿದೆ…

Find Minimum in Rotated Sorted Array II — ಪದೇ ಪದೇ ಕೇಳುವ ಪ್ರಶ್ನೆಗಳು

Find Minimum in Rotated Sorted Array II ಹಿಂದಿನ intuition ಏನು?

Same comparison with the right end, plus one case for duplicates: when nums[mid] equals nums[high] you can't tell which side holds the minimum, but you can safely drop high because mid has the same value.

Find Minimum in Rotated Sorted Array II (LeetCode 154) ಅನ್ನು ಹೇಗೆ ಪರಿಹರಿಸುವುದು?

1. nums[mid] > nums[high] → minimum is right: low = mid + 1. 2. nums[mid] < nums[high] → high = mid. 3. Equal → high −= 1. 4. Return nums[low].

Find Minimum in Rotated Sorted Array II ನ time ಮತ್ತು space complexity ಎಷ್ಟು?

Time: O(log n) average, O(n) worst. Space: O(1). (Accepted approach: Binary search with duplicates.)

Find Minimum in Rotated Sorted Array II ನಲ್ಲಿ ಸಾಮಾನ್ಯ ತಪ್ಪುಗಳು ಯಾವುವು?

Don't jump high = mid on equality — the minimum could be between.