2
def maxProfit(self, prices: List[int]) -> int:11
if (i, buying) in cache:12
return cache[(i, buying)]15
# if have sell the share in previous step16
# then currently we have two options17
# either buy or not buy(cooldown)19
# we have bought so, increment the index and set buying flag to not buying20
# and don't forget that we bought so, we have to reduce that share amount from profit21
buy = dfs(i + 1, not buying) - prices[i]23
cooldown = dfs(i + 1, buying)25
profit = max(buy, cooldown)26
cache[(i, buying)] = profit29
# we have sell the share so,30
# we cannot buy next share we have to skip the next price(cooldown for one day)31
# set (not buying) flag to buying32
# we also have to add that share price to profit33
sell = dfs(i + 2, not buying) + prices[i]35
cooldown = dfs(i + 1, buying)37
profit = max(sell, cooldown)38
cache[(i, buying)] = profit40
return cache[(i, buying)]