-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlags
52 lines (44 loc) · 1.21 KB
/
Flags
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
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
std::vector<int> peaks;
for (size_t i = 1; i < A.size() - 1; ++i) {
if (A[i - 1] < A[i] && A[i] > A[i + 1]) {
peaks.push_back(i);
}
}
int len = peaks.size();
if (len < 2) {
return len;
}
int first = *peaks.begin();
int last = *peaks.rbegin();
int flags = len;
for (; flags > 1; flags--) {
// Check difference, skip too big count
int diff = (last - first) / flags + 1;
if (diff < flags) {
continue;
}
// Check that all peaks have required distance
int left = peaks[0];
int peakCount = 1;
int skipCount = 0;
for (int i = 1; i < len; i++) {
if (abs(left - peaks[i]) >= flags) {
if (++peakCount == flags) {
break;
}
left = peaks[i];
}
else {
if (len - ++skipCount < flags) {
break;
}
}
}
if (peakCount == flags) {
break;
}
}
return flags;
}