Dynamic Programming 1
Dynamic Programming 1
if n == 1:
return 1
if n == 2:
return 2
prev1, prev2 = 2, 1
return prev1
Time Complexity:
Space Complexity:
● We only need to store the last two computed values (prev1 and prev2), so the space
complexity is O(1).
Solution:
def maxProfit(prices):
max_profit = 0
return max_profit
Time Complexity:
● O(n), where n is the length of the prices array. We loop through the list once, checking
each day.
Space Complexity:
● O(1). We only use a single integer max_profit to store the result, so the space
complexity is constant.