Skip to content

Instantly share code, notes, and snippets.

@Atlantic777
Created April 6, 2015 19:24
Show Gist options
  • Save Atlantic777/a2dcb6ee7ee54c195251 to your computer and use it in GitHub Desktop.
Save Atlantic777/a2dcb6ee7ee54c195251 to your computer and use it in GitHub Desktop.
def find_local_min(A, start):
current = A[start]
cid = start
while cid < len(A)-1:
if current > A[cid+1]:
current = A[cid+1]
cid += 1
else:
break
return cid
def find_local_max(A, start):
current = A[start]
cid = start
while cid < len(A)-1:
if current < A[cid+1]:
current = A[cid+1]
cid += 1
else:
break
return cid
def calc(A, P, Q, R):
return min(A[P]-A[Q], A[R] - A[Q])
def solution(A):
c = 0
best = -1
P = find_local_max(A, c)
while 1:
Q = find_local_min(A, P)
R = find_local_max(A, Q)
current = calc(A, P, Q, R)
if current > best:
best = current
P = R
if len(A)-1 in [P, Q, R]:
break
return best
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment