forked from kothariji/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path(LEETCODE)search-in-rotated-sorted-array.cpp
92 lines (81 loc) · 2.22 KB
/
(LEETCODE)search-in-rotated-sorted-array.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// https://leetcode.com/problems/search-in-rotated-sorted-array/
class Solution {
public:
int calcMid(int left, int right) {
return left + (right - left)/2;
}
bool isBorder(vector<int>& nums, int i) {
if(nums[i] > nums[i+1])
return true;
return false;
}
int binarySearch(vector<int>& nums, int left, int right, int target) {
int i = calcMid(left, right);
while(nums[i] != target) {
if(nums[i] < target)
left = i;
else
right = i;
i = calcMid(left, right);
if(right - left < 2 && nums[i] != target) {
if(nums[left] == target)
return left;
else if(nums[right] == target)
return right;
else {
i = -1;
break;
}
}
}
return i;
}
int findArrayBorder(vector<int>& nums, int left, int right) {
int firstListMax = nums[nums.size()-1];
int i = calcMid(left, right);
while(!isBorder(nums, i)) {
if(nums[i] < firstListMax)
right = i;
else
left = i;
i = calcMid(left, right);
}
return i;
}
int search(vector<int>& nums, int target) {
// edge cases:
if(target == nums[nums.size()-1])
return nums.size()-1;
if(target == nums[0])
return 0;
if(nums.size() == 1)
return -1;
int length = nums.size();
int firstListBegin = 0, firstListEnd;
int secondListBegin, secondListEnd = length - 1;
int targetIdx = -1;
//check whether the array has been rotated
if(nums[0] > nums[length-1]) {
firstListEnd = findArrayBorder(nums, 0, length-1);
secondListBegin = firstListEnd + 1;
//edge case
if(secondListBegin == secondListEnd) {
if(target == nums[secondListBegin])
return secondListBegin;
targetIdx = binarySearch(nums, 0, firstListEnd, target);
}
else if(target <= nums[secondListEnd]) {
// list 2
targetIdx = binarySearch(nums, secondListBegin, secondListEnd, target);
}
else {
// list 1
targetIdx = binarySearch(nums, 0, firstListEnd, target);
}
}
else {
targetIdx = binarySearch(nums, 0, length - 1, target);
}
return targetIdx;
}
};