-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags.js
52 lines (43 loc) · 1.37 KB
/
flags.js
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
// -----------------------------------------------------------------------------
// FLAGS
// Find the maximum number of flags that can be set on mountain peaks.
//
// Report: https://app.codility.com/demo/results/training9UNPF6-M7X/
// -----------------------------------------------------------------------------
function solution(A) {
if (A.length < 3) return 0 // no peaks
let peaks = []
// find all peaks
for (let i=1; i < A.length-1; ++i) {
if (A[i] > A[i-1] && A[i] > A[i+1]) {
peaks.push(i)
}
}
if (peaks.length == 0) {
return 0
}
let k = 1
let max_flags = 1
// max flags ~ sqrt(N)
while ((k-1) * k <= A.length) {
let flags = [peaks[0]]
let i = 1;
while (i < peaks.length) {
if (peaks[i] - flags[flags.length-1] >= k) {
flags.push(peaks[i])
if (flags.length >= k) break;
}
i += 1
}
max_flags = Math.max(flags.length, max_flags)
k += 1
}
return max_flags
}
console.log(solution([1])) // 0
console.log(solution([1, 2])) // 0
console.log(solution([1, 4, 2])) // 1
console.log(solution([3, 2, 1])) // 0
console.log(solution([1, 5, 3, 4, 3, 4, 1, 2, 3])) // 2
console.log(solution([1, 5, 3, 4, 3, 4, 1, 2, 3, 4, 6, 2])) // 3
console.log(solution([0, 0, 0, 0, 0, 1, 0, 1, 0, 1])) // 2