LeetCode - Search in Rotated Sorted Array
2013.12.14 18:29
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7
might become 4 5 6 7 0 1 2
).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
Solution1:
My first solution is to find the smallest element in the array first, and do an "offset" binary search.
Finding the smallest element requires O(n) time, the rotated binary search requires O(log(n)) time.
Thus time complexity is O(n), space complexity is O(1).
Accepted code:
1 // 1AC, simple variation of binary search 2 class Solution { 3 public: 4 int 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 -1; 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 mid % n; 31 } 32 } 33 34 return -1; 35 } 36 };
Solution2:
Apparently you won't impress anyone with an O(n) solution when the data given is somewhat "sorted", right? Let's find out how to make it O(log(n)).
See the three examples below:
1 2 3 4 5 6 7
[7 1] 2 3 4 5 6
4 5 6 [7 1] 2 3
The three examples above are all rotated sorted array, rotated by 0, 1, 4 elements. For the latter two, the pair marked with "[]" is where the minimal element is followed by the maximal, that's where the pivot is. We're supposed to find it out using O(log(n)) time. Then do the binary search in an extra O(log(n)) time.
See the code below, time complexity is O(log(n)), space complexity is O(1).
Accepted code:
1 // 1AC, simple variation of binary search, time complexity O(log(n)) 2 class Solution { 3 public: 4 int 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 -1; 10 } 11 12 int left, mid, right; 13 if(A[0] < A[n - 1]){ 14 offset = n - 1; 15 }else{ 16 17 left = 0; 18 right = n - 1; 19 while(right - left > 1){ 20 mid = (left + right) / 2; 21 if(A[left] > A[mid]){ 22 right = mid; 23 }else{ 24 left = mid; 25 } 26 } 27 offset = left; 28 } 29 30 offset = (offset + 1) % n; 31 left = offset; 32 right = n - 1 + offset; 33 while(left <= right){ 34 mid = (left + right) / 2; 35 if(target < A[mid % n]){ 36 right = mid - 1; 37 }else if(target > A[mid % n]){ 38 left = mid + 1; 39 }else{ 40 return mid % n; 41 } 42 } 43 44 return -1; 45 } 46 };
Solution 3:
The solution above is already O(log(n)) in time, but performed 2 binary searches, let's try do this in one pass.
Note that whenever you do a binary search, your goal is to limit the target within one interval, and shrink the size of interval with every iteration.
So we'll need a few more "if" statements to ensure that the target value doesn't run out of the searching scope.
Time complexity is O(log(n)), space complexity is O(1). This version of code is not very easy to understand, thus I'd still prefer the 2nd version, as simple code is easy to understand and maintain.
Accepted code:
1 // 1AC, simple variation of binary search, the code is not beautiful, though... 2 class Solution { 3 public: 4 int 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 -1; 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 if(A[left] <= A[right]){ 19 // the interval is already monotonous 20 if(target < A[mid]){ 21 right = mid - 1; 22 }else if(target > A[mid]){ 23 left = mid + 1; 24 }else{ 25 return mid; 26 } 27 }else{ 28 // the interval includes the 'pivot', containing two monotonous sub-array 29 if(target < A[mid]){ 30 if(target >= A[left]){ 31 right = mid - 1; 32 }else{ 33 if(A[mid] >= A[left]){ 34 left = mid + 1; 35 }else{ 36 right = mid - 1; 37 } 38 } 39 }else if(target > A[mid]){ 40 if(target <= A[right]){ 41 left = mid + 1; 42 }else{ 43 if(A[mid] >= A[left]){ 44 left = mid + 1; 45 }else{ 46 right = mid - 1; 47 } 48 } 49 }else{ 50 return mid; 51 } 52 } 53 } 54 55 return -1; 56 } 57 };