-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8-Peaks.rb
More file actions
29 lines (25 loc) · 872 Bytes
/
8-Peaks.rb
File metadata and controls
29 lines (25 loc) · 872 Bytes
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
# you can use puts for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a)
if a.count >= 3
peaks = [0] * (a.count + 1)
(1...a.count - 1).each { |i|
peaks[i + 1] = peaks[i]
peaks[i + 1] += 1 if a[i] > a[i - 1] and a[i] > a[i + 1]
}
peaks[a.count] = peaks[a.count - 1]
block_sizes = (2..Math.sqrt(a.count).floor).select { |i|
a.count % i == 0
}.flat_map { |i|
[i, a.count / i]
}.sort.uniq
block_sizes << a.count
block_sizes.each { |block_size|
all_with_a_peak = (0...a.count).step(block_size).all? { |block_start|
peaks[block_start + block_size] > peaks[block_start]
}
return a.count / block_size if all_with_a_peak
}
end
return 0
end