力扣 309. 最佳买卖股票时期含冷冻期

解法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
def maxProfit(prices):
# 动态规划
"""
dp_0表示手中持有股票的状态
dp_1表示手中没有股票,但处于冷冻期的状态
dp_2表示手中没有股票,也没有处于冷冻期的状态
"""
dp_0, dp_1, dp_2 = -prices[0], 0, 0
for i in range(1, len(prices)):
cur_0 = max(dp_0, dp_2-prices[i])
cur_1 = dp_0 + prices[i]
cur_2 = max(dp_1, dp_2)
dp_0, dp_1, dp_2 = cur_0, cur_1, cur_2
return max(dp_1, dp_2)
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2022 eightyninth
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信