C++ Peaks
#include <algorithm>
#include <vector>
int peaks(const std::vector<int>& a)
{
int n = static_cast<int>(a.size());
if (n <= 2) {
return 0;
}
std::vector<int> sum(n, 0);
int last = -1;
int dist = 0;
for (int i = 1; i + 1 < n; ++i) {
sum[i] = sum[i - 1];
if (a[i] > a[i - 1] && a[i] > a[i + 1]) {
dist = std::max(dist, i - last);
last = i;
++sum[i];
}
}
sum[n - 1] = sum[n - 2];
if (sum[n - 1] == 0) {
return 0;
}
dist = std::max(dist, n - last);
int j = 0;
for (int i = (dist >> 1) + 1; i < dist; ++i) {
if (n % i == 0) {
last = 0;
for (j = i; j <= n; j += i) {
if (sum[j - 1] <= last) {
break;
}
last = sum[j - 1];
}
if (j > n) {
return n / i;
}
}
}
for (last = dist; n % last != 0;) {
++last;
}
return n / last;
}
This finds the peak positions, then tests how many equal blocks can each contain at least one peak.