-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindaPeakElementII.cpp
More file actions
43 lines (40 loc) · 1.12 KB
/
findaPeakElementII.cpp
File metadata and controls
43 lines (40 loc) · 1.12 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
42
43
// Source: https://leetcode.com/problems/find-a-peak-element-ii/
// Author: Miao Zhang
// Date: 2021-06-17
class Solution {
public:
vector<int> findPeakGrid(vector<vector<int>>& mat) {
int m = mat.size();
int n = mat[0].size();
int l = 0;
int r = m - 1;
auto findMax = [&] (int k) {
vector<int> ans(2);
ans[0] = k;
int up = k - 1 >= 0 ? k - 1: k;
int down = k + 1 < m ? k + 1: k;
for (int i = up; i <= down; i++) {
for (int j = 0; j < n; j++) {
if (mat[ans[0]][ans[1]] < mat[i][j]) {
ans[0] = i;
ans[1] = j;
}
}
}
return ans;
};
vector<int> res(2);
while (l < r) {
int mid = l + (r - l) / 2;
res = findMax(mid);
if (res[0] == mid) {
return res;
} else if (res[0] < mid) {
r = mid - 1;
} else {
l = mid + 1;
}
}
return res;
}
};