121. 买卖股票的最佳时机
class Solution(object):def maxProfit(self, prices):""":type prices: List[int]:rtype: int"""if len(prices) == 0:return 0dp = [[0]*2 for i in range(len(prices))]dp[0][0]=-prices[0]dp[0][1]=0for i in range(1, len(prices)):dp[i][1]=max(dp[i-1][1], dp[i-1][0]+prices[i])dp[i][0]=max(dp[i-1][0], -prices[i])return dp[-1][1]
122.买卖股票的最佳时机II
class Solution(object):def maxProfit(self, prices):""":type prices: List[int]:rtype: int"""dp = [[0]*2 for i in range(len(prices))]dp[0][0]=-prices[0]dp[0][1]=0for j in range(1, len(prices)):dp[j][1]=max(dp[j-1][0]+prices[j],dp[j-1][1])dp[j][0]=max(dp[j-1][1]-prices[j],dp[j-1][0])return dp[-1][1]
123.买卖股票的最佳时机III
class Solution(object):def maxProfit(self, prices):""":type prices: List[int]:rtype: int"""dp = [[0]*5 for i in range(len(prices))]dp[0][0]=0dp[0][1]=-prices[0]dp[0][2]=0dp[0][3]=-prices[0]dp[0][4]=0for j in range(1, len(prices)):dp[j][0]=0dp[j][1]=max(dp[j-1][1], dp[j-1][0]-prices[j])dp[j][2]=max(dp[j-1][2], dp[j-1][1]+prices[j])dp[j][3]=max(dp[j-1][3], dp[j-1][2]-prices[j])dp[j][4]=max(dp[j-1][4], dp[j-1][3]+prices[j])return dp[-1][4]