Update 0309.最佳买卖股票时机含冷冻期.md

This commit is contained in:
Baturu
2021-06-07 21:52:32 -07:00
committed by GitHub
parent a4b7399acb
commit eeefc8499b

View File

@ -189,6 +189,21 @@ class Solution {
Python
```python
class Solution:
def maxProfit(self, prices: List[int]) -> int:
n = len(prices)
if n == 0:
return 0
dp = [[0] * 4 for _ in range(n)]
dp[0][0] = -prices[0] #持股票
for i in range(1, n):
dp[i][0] = max(dp[i-1][0], max(dp[i-1][3], dp[i-1][1]) - prices[i])
dp[i][1] = max(dp[i-1][1], dp[i-1][3])
dp[i][2] = dp[i-1][0] + prices[i]
dp[i][3] = dp[i-1][2]
return max(dp[n-1][3], dp[n-1][1], dp[n-1][2])
```
Go