Problem statement

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/

Solution

After evaluating differences like in 0121, we can just evaluate sum of all positive elements.

Complexity

It is O(n) for time and space. Space can be made O(1).

Code

class Solution:
    def maxProfit(self, nums):
        diffs = [y - x for x, y in zip(nums, nums[1:])]
        return sum(max(0, x) for x in diffs)