Created
April 6, 2015 19:24
-
-
Save Atlantic777/a2dcb6ee7ee54c195251 to your computer and use it in GitHub Desktop.
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
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