122. Best Time to Buy and Sell Stock II
class Solution:
def maxProfit(self, prices: List[int]) -> int:
# memo = {}
# def traverse(i, canBuy):
# if i >= len(prices):
# return 0
# if (i, canBuy) in memo:
# return memo[(i, canBuy)]
# if canBuy:
# profit = max(traverse(i + 1, False) - prices[i], traverse(i + 1, True))
# else:
# profit = max(traverse(i + 1, True) + prices[i], traverse(i + 1, False))
# memo[(i, canBuy)] = profit
# return memo[(i, canBuy)]
# return traverse(0, True)
maxProfit = 0
for i in range(1, len(prices)):
if prices[i] > prices[i - 1]:
maxProfit += prices[i] - prices[i - 1]
return maxProfit