dp
]
Leetcode 0121. Best Time to Buy and Sell Stock
Classical dynamic programming problem. Let dp[i]
be a maximum profit we can have if we sell stock at i
-th moment of time. Then we can get, that dp[i+1] = max(dp[i] + q, q)
, where q = nums[i+1] - nums[i]
, we have two choices, either we just buy and immeditely sell and get q
gain, or we use dp[i] + q
to merge two transactions.
Note now, that we do not really need to keep all dp
array, but we can keep only last state.
Complexity: time complexity is O(n)
, space complexity is O(1)
.
class Solution:
def maxProfit(self, nums):
ans, dp = 0, 0
for i in range(0, len(nums)-1):
q = nums[i+1] - nums[i]
dp = max(dp + q, q)
ans = max(ans, dp)
return ans
PS Look also my solutions to similar problems: Best Time to Buy and Sell Stock III https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/discuss/794633/Python-O(n)-solution-with-optimization-explained Best Time to Buy and Sell Stock with Cooldown https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/discuss/761720/Python-dp-O(n)-solution-using-differences-explained