-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path162.cpp
More file actions
31 lines (26 loc) · 710 Bytes
/
162.cpp
File metadata and controls
31 lines (26 loc) · 710 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
// 162. Find Peak Element - https://leetcode.com/problems/find-peak-element
#include "bits/stdc++.h"
using namespace std;
class Solution {
public:
int findPeakElement(vector<int>& nums) {
int n = (int)nums.size();
int L = 0;
int R = n - 1;
if (n == 1) { return 0; }
// invariant: nums[L - 1] < nums[L] && nums[R] > nums[R + 1]
while (R - L > 1) {
int M = L + (R - L) / 2;
if (nums[M] < nums[M + 1]) {
L = M + 1;
} else {
R = M;
}
}
return (L == n - 1 || nums[L] > nums[R] ? L : R);
}
};
int main() {
ios::sync_with_stdio(false);
return 0;
}