forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearch in Rotated Sorted Array II.cpp
51 lines (41 loc) · 1.55 KB
/
Search in Rotated Sorted Array II.cpp
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
class Solution {
public:
bool search(vector<int>& nums, int target) {
if( nums[0] == target or nums.back() == target ) return true;
// this line is redundant it reduces only the worst case when all elements are same to O(1)
const int n = nums.size();
int l = 0 , h = n-1;
while( l+1 < n and nums[l] == nums[l+1]) l++;
// if all elements are same
if( l == n-1){
if( nums[0] == target ) return true;
else return false;
}
// while last element is equal to 1st element
while( h >= 0 and nums[h] == nums[0] ) h--;
int start = l , end = h;
// find the point of pivot ie from where the rotation starts
int pivot = -1;
while( l <= h ){
int mid = l + (h-l)/2;
if( nums[mid] >= nums[0] ) l = mid+1;
else {
pivot = mid;
h = mid-1;
}
}
if( pivot == -1 ) l = start , h = end; // if no pivot exits then search space is from start -e end
else {
if( target > nums[end] ) l = start , h = pivot-1; // search space second half
else l = pivot , h = end; // search space first half
}
// normal binary search
while ( l <= h ){
int mid = l + (h-l)/2;
if( nums[mid] > target ) h = mid-1;
else if( nums[mid] < target ) l = mid+1;
else return true;
}
return false;
}
};