代码随想录算法训练营Day 50 | 动态规划part12 | 309.最佳买卖股票时机含冷冻期、714.买卖股票的最佳时机含手续费
文章目录
309.最佳买卖股票时机含冷冻期
题目链接
一、两种状态
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
# dp[i][0], 第i天【持有股票】所得的最大利润
# dp[i][1], 第i天【不持有股票】所得的最大利润
if len(prices)<2:
return 0
dp = [[0]*2 for _ in range(len(prices))]
dp[0][0] = -prices[0] # 第1天买入
dp[1][0] = max(-prices[0],-prices[1]) # 第2天买入或持有
dp[0][1] = 0 # 第1天不持有
dp[1][1] = max(0,prices[1