Skip to content

Latest commit

 

History

History
executable file
·
96 lines (86 loc) · 2.93 KB

0081. Search in Rotated Sorted Array II.md

File metadata and controls

executable file
·
96 lines (86 loc) · 2.93 KB

81. Search in Rotated Sorted Array II, medium

tags: review

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).

You are given a target value to search. If found in the array return true, otherwise return false.

Example 1:

Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Example 2:

Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Follow up:

This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.
Would this affect the run-time complexity? How and why?
// 4ms, 98%
class Solution {
private:
    bool searchHelper(vector<int>& nums, int target, int low, int high) {
        while (low <= high) {
            int m = low + (high-low)/2;
            if (nums[m] == target) return true;
            if (low == high) return false;

            // left part is sorted
            if (nums[low] < nums[m]) {
                if (target >= nums[low] && target <= nums[m])
                    high = m - 1;
                else 
                    low = m + 1;
            } else if (nums[high] > nums[m]) {
                if (target >= nums[m] && target <= nums[high])
                    low = m + 1;
                else 
                    high = m - 1;
            } else {
                // [low] == [m] or [m] == [high] or both
                while (low < high && nums[low] == nums[m]) low++;
                while (low < high && nums[high] == nums[m]) high--;
            }
        }
        return false;
    }
public:
    bool search(vector<int>& nums, int target) {
        int low = 0, high = nums.size() - 1;
        return searchHelper(nums, target, low, high);
    }
};

// 8 ms
class Solution {
private:
    bool searchHelper(vector<int>& nums, int target, int low, int high) {
        if (low <= high) {
            int m = low + (high-low)/2;
            if (target == nums[m])
                return true;
            
            // left is sorted
            if (nums[low] < nums[m]) {
                if (target < nums[m] && target >= nums[low]) {
                    return searchHelper(nums, target, low, m-1);
                } else {
                    return searchHelper(nums, target, m+1, high);
                }
                // right is sorted
            } else if (nums[m] < nums[high]) { 
                if (nums[m] < target && target <= nums[high]) {
                    return searchHelper(nums, target, m+1, high);
                } else {
                    return searchHelper(nums, target, low, m-1);
                }
            } else {
                return searchHelper(nums, target, low, m-1) ||
                    searchHelper(nums, target, m+1, high);
            }
        }
        return false;
    }
public:
    bool search(vector<int>& nums, int target) {
        int low = 0, high = nums.size() - 1;
        return searchHelper(nums, target, low, high);
    }
};