class Solution:
def maxProfit(self, prices: List[int]) -> int:
if prices==[]:
return 0
leng=len(prices)
dp=[ [[0,0,0],[0,0,0] ] for i in range(leng) ]
dp[0][0][0]=0
dp[0][1][0]=-prices[0]
dp[0][0][1] = float('-inf')
dp[0][0][2] = float('-inf')
dp[0][1][1]=float('-inf')
dp[0][1][2]=float('-inf')
for i in range(1,leng):
dp[i][0][0]=0
dp[i][0][1]=max(dp[i-1][1][0]+prices[i],dp[i-1][0][1])
dp[i][0][2]=max(dp[i-1][1][1]+prices[i],dp[i-1][0][2])
dp[i][1][0]=max(dp[i-1][0][0]-prices[i],dp[i-1][1][0])
dp[i][1][1]=max(dp[i-1][0][1]-prices[i],dp[i-1][1][1])
dp[i][1][2]=float('-inf')
return max(dp[leng-1][0][1],dp[leng-1][0][2],0)
打卡
最新推荐文章于 2024-05-28 08:21:18 发布