-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathsolution162.cpp
More file actions
45 lines (37 loc) · 889 Bytes
/
solution162.cpp
File metadata and controls
45 lines (37 loc) · 889 Bytes
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
/**
* Find Peak Element
* Binary search, theroy is base on mit course peak find course lecture 1.
* (http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/lecture-videos/MIT6_006F11_lec01.pdf)
* cpselvis([email protected])
* September 20th, 2016
*/
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
int findPeakElement(vector<int>& nums) {
if (nums.size() <= 1) return 0;
int l = 0, r = nums.size() -1;
while (l < r)
{
int mid = (l + r) >> 1;
if (nums[mid] > nums[mid + 1])
{
r = mid;
}
else if (nums[mid] < nums[mid + 1])
{
l = mid + 1;
}
}
return l;
}
};
int main(int argc, char **argv)
{
Solution s;
int arr[1] = {1};
vector<int> vec(arr + 0, arr + 1);
cout << s.findPeakElement(vec) << endl;
}