-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPeaks
46 lines (39 loc) · 1.45 KB
/
Peaks
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
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
auto N = A.size();
vector<int> peaks;
for (size_t i = 1; i < N - 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;
}
// from the "biggest possible number" to smaller number
for(int numBlocks = N; numBlocks >= 1; numBlocks--){
if (N % numBlocks == 0){ // it is divisible
int blockSize = N / numBlocks;
int ithOkBlock = 0; // the ith block has peak(s)
// test all the peaks
// if a peak is found in the ith block
// then, go to the (i+1)th block
for(int peaksIndex : peaks){
if( peaksIndex / blockSize == ithOkBlock){ // peak in the ith block
ithOkBlock++; // go to check (i+1)th block
}
}
// ithOkBlock: the number of blocks having peak(s)
// if all the blocks have peak(s)
// then, return the number of blocks
// note: we test from the biggest possible number
// so, once we find it, we can just return it
// (no need to check the smaller possible numbers)
if (ithOkBlock == numBlocks){
return numBlocks;
}
}
}
return 0;
}