Problem statement

https://binarysearch.com/problems/Wolf-of-Wall-Street/

Solution

Equal to leetcode 0121. Best Time to Buy and Sell Stock

Complexity

It is O(n) for time and O(1) for space.

Code

class Solution:
    def solve(self, nums):
        ans, dp = 0, 0
        for i in range(0, len(nums)-1):
            q = nums[i+1] - nums[i]
            dp = max(dp + q, q)
            ans = max(ans, dp)
        return ans