https://codility.com/demo/take-sample-test/peaks
http://blog.csdn.net/caopengcs/article/details/17491791
其实可以做到O(n)
#include <iostream>
#include <sstream>
using namespace std; int solution(vector<int> &A) {
int n = A.size();
int prevPeak = -1;
int maxDist = -1;
vector<int> peaks(n);
for (int i = 0; i < n; i++) {
if (i == 0) {
peaks[i] = 0;
} else if (i == n - 1) {
peaks[i] = peaks[i - 1];
} else if (A[i] > A[i - 1] && A[i] > A[i + 1]) {
peaks[i] = peaks[i - 1] + 1;
if (prevPeak == -1) {
prevPeak = i;
} else {
maxDist = max(maxDist, i - prevPeak);
prevPeak = i;
}
} else {
peaks[i] = peaks[i - 1];
}
}
if (peaks[n - 1] == 0) return 0;
if (peaks[n - 1] == 1) return 1;
for (int dist = maxDist / 2; dist <= n; dist++) {
if (n % dist != 0)
continue;
bool found = true;
for (int i = n - 1; i - dist > 0; i -= dist) {
if (peaks[i] == peaks[i - dist]) {
found = false;
break;
}
}
if (found) return n / dist;
}
return 0;
}