题解 | #买卖股票的最好时机(三)#
买卖股票的最好时机(三)
https://www.nowcoder.com/practice/4892d3ff304a4880b7a89ba01f48daf9
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 两次交易所能获得的最大收益
# @param prices int整型一维数组 股票每一天的价格
# @return int整型
#
class Solution:
def maxProfit(self , prices: List[int]) -> int:
# write code here
#如果前一天有股票,今日有交易
length=len(prices)
dp=[[-10000000]*5 for i in range(length)]
dp[0][0]=0
dp[0][1]=-prices[0]
for i in range(1,length):
dp[i][0]=dp[i-1][0]
#买进一只股票,无卖出
dp[i][1]=max(dp[i-1][1],dp[i-1][0]-prices[i])
#买进一只股票,卖出一只
dp[i][2]=max(dp[i-1][2],dp[i-1][1]+prices[i])
#买进二只股票,卖出一只
dp[i][3]=max(dp[i-1][3],dp[i-1][2]-prices[i])
#买进俩只股票,卖出俩只
dp[i][4]=max(dp[i-1][4],dp[i-1][3]+prices[i])
return max(dp[length-1][2],dp[length-1][4])

海康威视公司福利 1277人发布