mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Merge pull request #357 from z80160280/z80160280-patch-6
Update 0309.最佳买卖股票时机含冷冻期.md
This commit is contained in:
@ -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:
|
||||
|
||||
|
Reference in New Issue
Block a user