-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathBest Time to Buy and Sell Stock with Cooldown.py
43 lines (29 loc) · 1.5 KB
/
Best Time to Buy and Sell Stock with Cooldown.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Solution:
def maxProfit(self, prices: List[int]) -> int:
cache = {}
def dfs(i, buying):
if i >= len(prices):
return 0
if (i, buying) in cache:
return cache[(i, buying)]
if buying:
# if have sell the share in previous step
# then currently we have two options
# either buy or not buy(cooldown)
# we have bought so, increment the index and set buying flag to not buying
# and don't forget that we bought so, we have to reduce that share amount from profit
buy = dfs(i+1, not buying) - prices[i]
cooldown = dfs(i+1, buying)
profit = max( buy, cooldown )
cache[(i, buying)] = profit
else:
# we have sell the share so,
# we cannot buy next share we have to skip the next price(cooldown for one day)
# set (not buying) flag to buying
# we also have to add that share price to profit
sell = dfs(i+2, not buying) + prices[i]
cooldown = dfs(i+1, buying)
profit = max( sell, cooldown )
cache[(i, buying)] = profit
return cache[(i, buying)]
return dfs(0, True)