Skip to content

Instantly share code, notes, and snippets.

@medvednikov
Last active April 10, 2017 16:24
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 medvednikov/b77d695658b1c40943ed10328bba6e17 to your computer and use it in GitHub Desktop.
Save medvednikov/b77d695658b1c40943ed10328bba6e17 to your computer and use it in GitHub Desktop.
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