Last active
August 24, 2018 09:28
-
-
Save dbhalling/696d158df9dfc75ff43f7dbadd76e86f to your computer and use it in GitHub Desktop.
#codility30 Flags
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#codility30 Flags | |
a = [1, 5, 3, 4, 3, 4, 1, 2, 3, 4, 6, 2] | |
def solution(a) | |
count = a.count | |
peaks = [] | |
i = 0 | |
while i < (count - 1) | |
if (a[i + 1] > a[i]) and (a[i + 1] > a[i + 2]) | |
peaks = peaks.push(i + 1) | |
i += 2 | |
else | |
i += 1 | |
end | |
end | |
peakscount = peaks.count | |
j = 0 | |
while (j + 1) < peakscount | |
if (peaks[j + 1] - peaks[j]) < peakscount | |
peaks.delete_at(j + 1) | |
peakscount -= 1 | |
else | |
j += 1 | |
end | |
j += 1 | |
end | |
maxflags = peaks.count | |
return maxflags | |
end | |
puts "The maximum number of flags is #{solution(a)}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment