From 037759f87948c2ad7f8e9bcf94c28f22a0f3472d Mon Sep 17 00:00:00 2001 From: Samir Paul <77569653+SamirPaulb@users.noreply.github.com> Date: Mon, 5 Aug 2024 22:03:07 +0530 Subject: [PATCH] Update 02. As Many Transactions As You Like.py --- .../02. As Many Transactions As You Like.py | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/02_Dynamic-Programming/11. Buy and Sell Stock Problems/02. As Many Transactions As You Like.py b/02_Dynamic-Programming/11. Buy and Sell Stock Problems/02. As Many Transactions As You Like.py index 6e05d842..0318504a 100644 --- a/02_Dynamic-Programming/11. Buy and Sell Stock Problems/02. As Many Transactions As You Like.py +++ b/02_Dynamic-Programming/11. Buy and Sell Stock Problems/02. As Many Transactions As You Like.py @@ -1,4 +1,41 @@ # https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ +# https://www.youtube.com/watch?v=nGJmxkUJQGs + +class Solution: + def maxProfit(self, prices: List[int]) -> int: + memo = {} + def solve(i, can_sell): + if i == len(prices): + return 0 + if (i, can_sell) in memo: + return memo[(i, can_sell)] + if can_sell == 1: + profit = max(prices[i] + solve(i+1, 0), solve(i+1, 1)) + else: + profit = max(-prices[i] + solve(i+1, 1), solve(i+1, 0)) + memo[(i, can_sell)] = profit + return profit + return solve(0, 0) + +# Time: O(2 * N) +# Space: O(N + N) + +################################################################################################ + +class Solution: + def maxProfit(self, prices: List[int]) -> int: + n = len(prices) + dp = [[0]*2 for _ in range(n+1)] + dp[0][0] = -2**31 + for i in range(1, n+1): + for can_sell in range(2): + if can_sell == 1: + dp[i][can_sell] = max(prices[i-1] + dp[i-1][0], dp[i-1][1]) + else: + dp[i][can_sell] = max(-prices[i-1] + dp[i-1][1], dp[i-1][0]) + return dp[-1][1] + +################################################################################################ class Solution: def maxProfit(self, prices):