Skip to content

Instantly share code, notes, and snippets.

@dbhalling
Last active August 24, 2018 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbhalling/7ab3336fc92c5980797587caf792f958 to your computer and use it in GitHub Desktop.
Save dbhalling/7ab3336fc92c5980797587caf792f958 to your computer and use it in GitHub Desktop.
#codility30 Flags
#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