-
Notifications
You must be signed in to change notification settings - Fork 3
/
852.cpp
33 lines (32 loc) · 841 Bytes
/
852.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
class Solution {
public:
int peakIndexInMountainArray(vector<int>& A) {
int left = 1;
int right = A.size() - 2;
while (left <= right) {
int mid = left + (right - left) / 2;
if (A[mid] > A[mid - 1] && A[mid] > A[mid + 1]) return mid;
if (A[mid] > A[mid - 1]) {
left = mid + 1;
}
else {
right = mid;
}
}
return left;
}
};
// v2
// class Solution {
// public:
// int peakIndexInMountainArray(vector<int>& A) {
// int left = 0;
// int right = A.size() - 1;
// while (left < right) {
// int mid = left + (right - left) / 2;
// if (A[mid] > A[mid + 1]) right = mid;
// else left = mid + 1;
// }
// return left;
// }
// };