-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindinMountainArray.cpp
More file actions
41 lines (38 loc) · 1.22 KB
/
findinMountainArray.cpp
File metadata and controls
41 lines (38 loc) · 1.22 KB
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
// Source: https://leetcode.com/problems/find-in-mountain-array/
// Author: Miao Zhang
// Date: 2021-04-08
/**
* // 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) {
auto binary_search = [&](int l, int r, function<bool(int)> cond) {
while (l < r) {
int m = l + (r - l) / 2;
if (cond(m)) r = m;
else l = m + 1;
}
return l;
};
int n = mountainArr.length();
int p = binary_search(0, n - 1, [&](int i) -> bool {
return mountainArr.get(i) > mountainArr.get(i + 1);
});
int l = binary_search(0, p, [&](int i) -> bool {
return mountainArr.get(i) >= target;
});
if (mountainArr.get(l) == target) return l;
int r = binary_search(p, n - 1, [&](int i) -> bool {
return mountainArr.get(i) <= target;
});
if (mountainArr.get(r) == target) return r;
return -1;
}
};