-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPeak Element.cpp
39 lines (37 loc) · 1 KB
/
Peak Element.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
class Solution {
public:
int findPeakElement(vector<int>& nums) {
if(nums.size()==1){
return 0;
}
int low=0;
int high=nums.size()-1;
while(low<=high){
int mid=low+(high-low)/2 ;
if(mid>0 && mid<nums.size()-1){
if(nums[mid]>nums[mid+1] && nums[mid]>nums[mid-1]){
return mid;
}
else if(nums[mid]<nums[mid-1]){
high=mid-1;
}
else if(nums[mid] < nums[mid+1]){
low=mid+1;
}
}
else if(mid==0 ){
if(nums[mid]<nums[1]){
return 1;
}
return mid;
}
else if(mid==nums.size()-1 ){
if(nums[mid]>nums[mid-1]){
return mid;
}
return mid-1;
}
}
return 0;
}
};