对于上述的描述而言,我们有两种思路:
DP
的方式进行动态规划转移DP
较为感兴趣,所以我们使用第二种方法来解题,解题的思路如下:max profit
,而这个最大利润我们简记为MP
,那么MP
的动态转移方程就与前一天的动态转移方程有关,所以 M P [ i ] = M P [ i − 1 ] + i s b u y o r n o t MP[i]=MP[i-1]+isbuyornot MP[i]=MP[i−1]+isbuyornot 这里的 i s b u y o r n o t isbuyornot isbuyornot 非常麻烦,所以在MP
坐标轴上引入多一维的空间表达,这个空间表达为 M P [ i ] [ j ] MP[i][j] MP[i][j] ,那么 M P [ i ] [ j ] MP[i][j] MP[i][j] 中的 j j j 代表着三种状态:
class Solution:
def maxProfit(self, prices: List[int]) -> int:
if not prices: return 0
res = 0
profit = [[0 for i in range(3)] for i in range(len(prices))]
profit[0][0], profit[0][1], profit[0][2] = 0, -prices[0], 0
for i in range(1, len(prices)):
profit[i][0] = 0
profit[i][1] = max(profit[i-1][1], profit[i-1][0] - prices[i])
profit[i][2] = profit[i-1][1] + prices[i]
res = max(res,profit[i][0], profit[i][1], profit[i][2])
return res
查看执行效率:
对于Python
而言,这可不是一个很光彩的执行效率,所以尽量的使用第一种思想来操作:
题目描述:
这里可以增加多一个维度,这个维度是买卖了多少次,也就是说,由原来的 M P [ i ] [ j ] MP[i][j] MP[i][j] 变换成 M P [ i ] [ j ] [ k ] MP[i][j][k] MP[i][j][k],其中 k k k 代表了买卖了多少次,方程也可以表达为 M P [ i ] [ k ] [ j ] MP[i][k][j] MP[i][k][j]:
M P [ i , k , 0 ] = { M P [ i , k , 0 ] = M P [ i − 1 , k , 0 ] M P [ i , k , 0 ] = M P [ i − 1 , k − 1 , 1 ] + a [ i ] MP[i, k, 0]=\left\{ \begin{aligned} MP[i, k, 0] & = & MP[i-1, k, 0] \\ MP[i, k, 0] & = & MP[i-1, k-1, 1] + a[i] \end{aligned} \right. MP[i,k,0]={MP[i,k,0]MP[i,k,0]==MP[i−1,k,0]MP[i−1,k−1,1]+a[i]
M P [ i , k , 1 ] = { M P [ i , k , 1 ] = M P [ i − 1 , k , 1 ] M P [ i , k , 1 ] = M P [ i − 1 , k − 1 , 0 ] − a [ i ] MP[i, k, 1]=\left\{ \begin{aligned} MP[i, k, 1] & = & MP[i-1, k, 1] \\ MP[i, k, 1] & = & MP[i-1, k-1, 0] - a[i] \end{aligned} \right. MP[i,k,1]={MP[i,k,1]MP[i,k,1]==MP[i−1,k,1]MP[i−1,k−1,0]−a[i]
最后状态: max M P [ n , 0 , . . . , k , 0 ] \max{MP[n, {0,...,k}, 0]} maxMP[n,0,...,k,0]
class Solution:
def maxProfit(self, prices: List[int]) -> int:
if not prices: return 0
profit = [[[0 for _ in range(2)] for _ in range(3)] for _ in range(len(prices))]
profit[0][0][0], profit[0][0][1] = 0, -prices[0]
profit[0][1][0], profit[0][1][1] = -sys.maxsize, -sys.maxsize
profit[0][2][0], profit[0][2][1] = -sys.maxsize, -sys.maxsize
for i in range(1, len(prices)):
profit[i][0][0] = profit[i-1][0][0]
profit[i][0][1] = max(profit[i-1][0][1], profit[i-1][0][0] - prices[i])
profit[i][1][0] = max(profit[i-1][1][0], profit[i-1][0][1] + prices[i])
profit[i][1][1] = max(profit[i-1][1][1], profit[i-1][1][0] - prices[i])
profit[i][2][0] = max(profit[i-1][2][0], profit[i-1][1][1] + prices[i])
end = len(prices) - 1
return max(profit[end][0][0], profit[end][1][0], profit[end][2][0])