We use peak value approach
to solve this question.
Recursion
TC: O(2^n)
SC: O(n) Stack Space
def buy_sell(prices):
def get_profit(ind, buy):
if ind == n:
return 0
if buy:
do_nothing = get_profit(ind - 1, buy)
buy_stock = -prices[ind] + get_profit(ind - 1, not buy)
profit = max(do_nothing, buy_stock)
else:
do_nothing = get_profit(ind - 1, buy)
sell_stock = prices[ind] + get_profit(ind - 1, not buy)
profit = max(do_nothing, sell_stock)
return profit
return get_profit(0, True)
TC: O(2*n)
SC: O(2*n) + O(n) ~> Recusion Stack
def buy_sell(prices):
dp = [[-1, -1] for _ in range(len(prices))]
def get_profit(ind, buy):
if ind == len(prices):
return 0
if dp[ind][buy] != -1:
return dp[ind][buy]
if buy:
do_nothing = get_profit(ind + 1, buy)
buy_stock = -prices[ind] + get_profit(ind + 1, 0)
profit = max(do_nothing, buy_stock)
else:
do_nothing = get_profit(ind + 1, buy)
sell_stock = prices[ind] + get_profit(ind + 1, 1)
profit = max(do_nothing, sell_stock)
dp[ind][buy] = profit
return dp[ind][buy]
return get_profit(0, 1)
TC: O(2*n)
SC: O(2*n) ~> Optimize to O(1)
def buy_sell(prices):
n = len(prices)
dp = [[-1, -1] for _ in range(n+1)]
dp[n][0] = dp[n][1] = 0
for ind in range(n-1, -1, -1):
for buy in range(2):
if buy:
do_nothing = dp[ind + 1][buy]
buy_stock = -prices[ind] + dp[ind + 1][0]
dp[ind][buy] = max(do_nothing, buy_stock)
else:
do_nothing = dp[ind + 1][buy]
sell_stock = prices[ind] + dp[ind + 1][1]
dp[ind][buy] = max(do_nothing, sell_stock)
return dp[0][1] # In the starting buy should be 1