Skip to content

Instantly share code, notes, and snippets.

@PeculiarE
Created March 10, 2026 12:16
Show Gist options
  • Select an option

  • Save PeculiarE/faac5a68e0474d1da2e6cf8d444a2f69 to your computer and use it in GitHub Desktop.

Select an option

Save PeculiarE/faac5a68e0474d1da2e6cf8d444a2f69 to your computer and use it in GitHub Desktop.
Search in Rotated Sorted Array II - LeetCode - Day 69

Question

Intuition

Approach

Complexity

  • Time complexity: $$O(n)$$

  • Space complexity: $$O(1)$$

Code

class Solution:
    def search(self, nums: List[int], target: int) -> bool:
        low, high = 0, len(nums) - 1
        while low <= high:
            mid = (low + high) // 2
            if nums[mid] == target:
                return True
            # Can't determine which half is sorted
            if nums[low] == nums[mid] == nums[high]:
                low += 1
                high -= 1
            # Left half is cleanly sorted
            elif nums[low] <= nums[mid]:
                if nums[low] <= target < nums[mid]:
                    high = mid - 1
                else:
                    low = mid + 1
            # Right half is cleanly sorted
            else:
                if nums[mid] < target <= nums[high]:
                    low = mid + 1
                else:
                    high = mid - 1
        return False

Result

Screenshot 2026-03-10 at 12 09 03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment