Let's explore 4 problems related to searching in rotated sorted arrays:
- LeetCode 33: Search in Rotated Sorted Array
- LeetCode 81: Search in Rotated Sorted Array II
- LeetCode 153: Find Minimum in Rotated Sorted Array
- LeetCode 154: Find Minimum in Rotated Sorted Array II
These can be categorized into three groups:
- 33, 81: Searching for a specific value
- 153, 154: Finding the minimum value
- 81, 154: Arrays with duplicate elements
Search in Rotated Sorted Array
The problem requires O(logn) time complexity, which suggests binary search. The binary search process involves continuously shrinking the search boundaries, and determining how to reduce the interval is crucial.
For an unrotated array, searching for a specific element target works as folllows:
- If target == nums[mid], return immediately
- If target < nums[mid], then target is in the left interval [left,mid). Set right = mid-1 and search in the left interval
- If target > nums[mid], then target is in the right interval (mid,right]. Set left = mid+1 and search in the right interval
However, in this problem, since the array is rotated, either the left or right interval may not be contiguous. How do we determine which interval contains the target?
Based on the properties of rotated arrays, **when elements are unique, if nums[i]