-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetCode1095.cpp
66 lines (65 loc) · 1.45 KB
/
LeetCode1095.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
/**
* // This is the MountainArray's API interface.
* // You should not implement it, or speculate about its implementation
* class MountainArray {
* public:
* int get(int index);
* int length();
* };
*/
class Solution {
public:
int findInMountainArray(int target, MountainArray &mountainArr) {
int s=0;
int e=mountainArr.length()-1;
int peakInd;
while(s<e)
{
int mid=(s+e)/2;
if(mountainArr.get(mid)<mountainArr.get(mid+1))
{
s=mid+1;
peakInd=s;
}
else e=mid;
}
int left=-1,right=-1;
s=0;
e=peakInd;
while(s<=e)
{
int mid=(s+e)/2;
int val=mountainArr.get(mid);
if(val==target)
{
left=mid;
}
if(val<target)
{
s=mid+1;
}
else
{
e=mid-1;
}
}
if(left!=-1 and mountainArr.get(left)==target) return left;
s=peakInd+1;
e=mountainArr.length()-1;
while(s<=e)
{
int mid=(s+e)/2;
int val=mountainArr.get(mid);
if(val==target)
{
right=mid;
}
if(target<val)
{
s=mid+1;
}
else e=mid-1;
}
return right;
}
};