-
Notifications
You must be signed in to change notification settings - Fork 17
/
best-time-to-buy-and-sell-stock-iii.py
52 lines (42 loc) · 1.65 KB
/
best-time-to-buy-and-sell-stock-iii.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
43
44
45
46
47
48
49
50
51
52
from typing import List
from functools import lru_cache
class Solution:
def maxProfit(self, prices: List[int]) -> int:
# return self.maxProfitDPTopDown(prices)
# return self.maxProfitDPBottomUp(prices)
def maxProfitDPBottomUp(self, prices: List[int]) -> int:
operations = 2
# 0 - bought, 1 - sold
dp = [
[[0] * (operations + 1) for _ in range(len(prices) + 1)],
[[0] * (operations + 1) for _ in range(len(prices) + 1)],
]
for price in reversed(range(len(prices))):
for left in range(operations + 1):
dp[0][price][left] = max(
dp[0][price + 1][left],
(dp[1][price + 1][left - 1] + prices[price]) if left > 0 else 0,
)
dp[1][price][left] = max(
dp[1][price + 1][left], dp[0][price + 1][left] - prices[price],
)
return dp[1][0][operations]
def maxProfitDPTopDown(self, prices: List[int]) -> int:
@lru_cache(None)
def dfs(bought: bool, price: int, left: int) -> int:
if price == len(prices) or left == 0:
return 0
result = 0
if bought:
result = max(
dfs(bought, price + 1, left),
dfs(not bought, price + 1, left - 1) + prices[price],
)
else:
result = max(
dfs(bought, price + 1, left),
dfs(not bought, price + 1, left) - prices[price],
)
return result
operations = 2
return dfs(False, 0, operations)