Search in Rotated Sorted Array II

2013.12.26 20:07

Follow up for "Search in Rotated Sorted Array":
What if duplicates are allowed?

Would this affect the run-time complexity? How and why?

Write a function to determine if a given target is in the array.

Solution1:

  First comes the link to the old original problem: Search in Rotated Sorted Array.

  The first solution wasn't quite satisfactory, with a time complexity of O(n). First we find out the rotation offset with O(n) time, and do a rotated binary search with another O(log(n)) time. Space complexity is O(1).

Accepted code:

 1 // 1AC, simple variation of binary search, even simpler
 2 class Solution {
 3 public:
 4     bool search(int A[], int n, int target) {
 5         // Note: The Solution object is instantiated only once and is reused by each test case.
 6         int offset;
 7         
 8         if(A == nullptr || n <= 0){
 9             return false;
10         }
11         
12         for(offset = 0; offset < n; ++offset){
13             if(A[offset] > A[(offset + 1) % n]){
14                 break;
15             }
16         }
17         
18         int left, mid, right;
19         
20         offset = (offset + 1) % n;
21         left = offset;
22         right = n - 1 + offset;
23         while(left <= right){
24             mid = (left + right) / 2;
25             if(target < A[mid % n]){
26                 right = mid - 1;
27             }else if(target > A[mid % n]){
28                 left = mid + 1;
29             }else{
30                 return true;
31             }
32         }
33         
34         return false;
35     }
36 };

Solution2:

  Think about this case: [2, 2, 2, 2, 0, 2, 2]

  If we apply the previous O(log(n)) solution from the original problem, there will be a problem:

    1. left = 0;

    2. right = 6;

    3. mid = 3;

    4. A[left] == A[mid], A[mid] == A[right]

    Then, how do I find out the rotation offset using bi-search if I'm unable to decide which path to go next?

  Thus we can apply binary search on an interval only when it is strictly ascending, use linear search otherwise.

  Time complexity is between O(log(n)) and O(n), space complexity is O(1).

Accepted code:

 1 // 1AC, not as efficient, but good solution
 2 class Solution {
 3 public:
 4     bool search(int A[], int n, int target) {
 5         // Note: The Solution object is instantiated only once and is reused by each test case.
 6         int offset;
 7         
 8         if(A == nullptr || n <= 0){
 9             return false;
10         }
11         
12         int left, mid, right;
13         
14         left = 0;
15         right = n - 1;
16         while(left <= right){
17             mid = (left + right) / 2;
18             // normal binary search cannot deal with cases like this
19             // [2, 2, 0, 0, 2, 2, 2]
20             if(A[left] < target && target < A[mid]){
21                 right = mid - 1;
22             }else if(A[mid] < target && target < A[right]){
23                 left = mid + 1;
24             }else{
25                 if(A[left] == target){
26                     return true;
27                 }else{
28                     ++left;
29                 }
30                 if(A[right] == target){
31                     return true;
32                 }else{
33                     --right;
34                 }
35             }
36         }
37         
38         return false;
39     }
40 };

 

 posted on 2013-12-26 20:13  zhuli19901106  阅读(187)  评论(0编辑  收藏  举报