[Hacker Rank] Stock Maximize

Stock Maximize

  • Time : O(n)
  • Space : O(1)
1
2
3
4
5
6
7
def stockmax(A):
res = 0
ma = 0
for i in range(len(A) - 1, -1, -1):
res += max(ma - A[i], 0)
ma = max(ma, A[i])
return res
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/06/16/PS/HackerRank/stockmax/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.