Time Complexity: O(N)
Space Complexity: O(1)
class Solution:
def maxProfit(self, prices: List[int]) -> int:
n = len(prices)
if n == 1:
return 0
cumulatProfit = 0
for i in range(1, n):
priceDiff = prices[i] - prices[i-1]
if priceDiff > 0:
cumulatProfit += priceDiff
return cumulatProfit
Time Complexity: O(N)
Space Complexity: O(1)