Last active
April 10, 2017 16:24
-
-
Save medvednikov/b77d695658b1c40943ed10328bba6e17 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
EDGE = 0 | |
ABOVE = 1 | |
BELOW = 2 | |
def solution(A): | |
res = 0 | |
prev = EDGE | |
for i in xrange(0, len(A)): | |
val = A[i] | |
if i == len(A) - 1: | |
return res + 1 | |
# Skip if on the same level | |
if i < len(A) and val == A[i+1]: | |
continue | |
# Valley | |
if (val < A[i+1] or i == len(A) - 1) and (prev == EDGE or prev == ABOVE): | |
res += 1 | |
prev = BELOW | |
# Hill | |
if (val > A[i+1] or i == len(A) - 1) and (prev == EDGE or prev == BELOW): | |
res += 1 | |
prev = ABOVE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment