-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch_In_Rotated_Array.cpp
More file actions
56 lines (46 loc) · 1.7 KB
/
Search_In_Rotated_Array.cpp
File metadata and controls
56 lines (46 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "vector";
using namespace std;
#include "iostream";
// Note: An O(n) solution that just iterates through the array seems to pass all the cases as well but doesn't meet
// the time complexity requirement of the problem.
class Solution {
public:
int search(vector<int>& nums, int target) {
if (nums[0] == target) {
return 0;
}
// Search for the index of the minimum value
int minIndex = searchIndex(nums, 0, nums.size()-1);
// Run binary search on the left or right subarray
if (nums[minIndex] <= target && nums[nums.size()-1] >= target) {
return binarySearch(nums, minIndex, nums.size()-1, target);
} else {
return binarySearch(nums, 0, minIndex-1, target);
}
}
int binarySearch(vector<int>& nums, int begin, int end, int target) {
int median = (begin + end) / 2;
if (begin > end) {
return -1;
}
if (nums[median] == target) {
return median;
} else if (target<nums[median]) {
return binarySearch(nums, begin, median-1, target);
} else {
return binarySearch(nums, median+1, end, target);
}
}
int searchIndex(vector<int>& nums, int begin, int end) {
int median = (begin + end) / 2;
if (nums[end] >= nums[median] && nums[begin] <= nums[median]) {
return begin;
} else if(median-1 >= begin && nums[median] < nums[median-1]) {
return median;
} else if(nums[median] > nums[end]) {
return searchIndex(nums, median+1, end);
} else {
return searchIndex(nums, begin, median-1);
}
}
};