Skip to content

Instantly share code, notes, and snippets.

@codecakes
Created October 7, 2021 01:22
Show Gist options
  • Save codecakes/1be4e9b0f64a2d1c15912c462ef65ced to your computer and use it in GitHub Desktop.
Save codecakes/1be4e9b0f64a2d1c15912c462ef65ced to your computer and use it in GitHub Desktop.
Given a log of stock prices compute the maximum possible earning.
def solution(A):
# write your code in Python 3.6
max_diff = float('-inf')
cur_sum = 0
n = len(A)
for idx, num in zip(range(1, n), A[1:]):
diff = num - A[idx-1]
if cur_sum > 0:
cur_sum += diff
else:
cur_sum = diff
# print('diff, cur_sum', diff, cur_sum)
max_diff = max(max_diff, cur_sum)
return max(max_diff, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment