-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPeakFinder2D.cpp
More file actions
66 lines (55 loc) · 1.59 KB
/
Copy pathPeakFinder2D.cpp
File metadata and controls
66 lines (55 loc) · 1.59 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//
// Created by user on 02.10.2018.
//
#include <iostream>
using namespace std;
int FindGlobalMax(int **matrix, int column, int count_rows) {
int index = 0;
int max = 0;
for (int i = 0; i < count_rows; i++) {
if (max < matrix[i][column]) {
max = matrix[i][column];
index = i;
}
}
return index;
}
int FindPeak(int **matrix, int n, int m, int left = 0, int right = -1) {
if (m < 0)
return 0;
if (right == -1) {
right = m - 1;
}
int mid = (left + right) / 2;
int globalMax = FindGlobalMax(matrix, mid, n);
if (
(globalMax > 1 && matrix[globalMax][mid] >= matrix[globalMax - 1][mid]) &&
(globalMax + 1 < n && matrix[globalMax][mid] >= matrix[globalMax + 1][mid]) &&
(mid - 1 > 0 && matrix[globalMax][mid] >= matrix[globalMax][mid - 1]) &&
(mid + 1 < m && matrix[globalMax][mid] >= matrix[globalMax][mid + 1])
) {
return matrix[globalMax][mid];
}
else if (mid > 0 && matrix[globalMax][mid - 1] > matrix[globalMax][mid]) {
right = mid;
return FindPeak(matrix, n, mid + 1, left, right);
}
else if (mid + 1 < m && matrix[globalMax][mid + 1] > matrix[globalMax][mid]) {
left = mid;
return FindPeak(matrix, n, m , left, right);
}
return matrix[globalMax][mid];
}
int main()
{
cout << "Hello";
int matrix[4][4]= {
{0,2,0,0},
{0,3,0,0},
{0,5,0,0},
{0,4,7,0}
};
int peak = FindPeak(matrix, 4, 4);
cout << "Peak is: " << peak;
return 0;
}