the description of the question is in the following link
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
My code
class Solution:
def maxProfit(self, prices: List[int]) -> int:
if len(prices) ==0 or len(prices) ==1:
return 0
else:
TotalProfit = 0
i = 0
while i < len(prices)-1:
Buy = prices[i]
Sell = prices[i+1]
PriceDiff = Sell - Buy
TotalProfit += max(PriceDiff,0)
i+=1
return TotalProfit
We can see the total profit as a cumulative daily return, the only difference is that whenever we encounter a negative daily return, we ignore the loss part. Hence we just need to add all the positive returns.