Created
October 7, 2021 01:22
-
-
Save codecakes/1be4e9b0f64a2d1c15912c462ef65ced to your computer and use it in GitHub Desktop.
Given a log of stock prices compute the maximum possible earning.
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
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