动态规划1
class Solution:
def maxProfit(self, prices: List[int]) -> int:
# 数组长度
n = len(prices)
if n < 2:
return 0
# 动态规划变量
# 第一次买的价格
first_price = prices[0]
# 第一次卖的收益
first_cell = 0
# 第二次买的价格
second_price = prices[0]
# 第二次卖
second_cell = 0
# 开始动态规划迭代
for i in range(1, n):
first_price = min(first_price, prices[i])
first_cell = max(first_cell, prices[i]-first_price)
second_price = min(second_price, prices[i]-first_cell)
second_cell = max(second_cell, prices[i]-second_price)
return second_cell
动态规划2
class Solution:
def maxProfit(self, prices: List[int]) -> int:
# 数组长度
n = len(prices)
if n < 2:
return 0
# 动态规划变量
# 第一次买的价格
first_price = prices[0]
# 第一次卖的收益
first_cell = 0
# 第二次买的价格
second_price = prices[0]
# 第二次卖
second_cell = 0
# 开始动态规划迭代
for i in range(1, n):
first_price = min(first_price, prices[i])
first_cell = max(first_cell, prices[i]-first_price)
second_price = min(second_price, prices[i]-first_cell)
second_cell = max(second_cell, prices[i]-second_price)
return second_cell
动态规划3
class Solution:
def maxProfit(self, prices: List[int]) -> int:
# 数组长度
n = len(prices)
if n < 2:
return 0
# 动态规划变量
# 第一次买
pre_first_buy = -prices[0]
# 第一次卖
pre_first_cell = 0
# 第二次买
pre_second_buy = -prices[0]
# 第二次卖
pre_second_cell = 0
# 开始动态规划迭代
for i in range(1, n):
first_buy = max(pre_first_buy, -prices[i])
first_cell = max(pre_first_cell, pre_first_buy+prices[i])
second_buy = max(pre_second_buy, pre_first_cell-prices[i])
second_cell = max(pre_second_cell, pre_second_buy+prices[i])
pre_first_buy = first_buy
pre_first_cell = first_cell
pre_second_buy = second_buy
pre_second_cell = second_cell
return second_cell
小黑生活
下午听老干部讲课,感觉干货满满
晚上食堂买一个烤肠和薯塔
回家涮羊肉,把牛排给涮了
泡一包养生茶
下午一个人健身房练练
健完身按个摩
吃个烤肠和干脆面
拜厄第21条学习打卡
麦当劳早餐
来杯酱香拿铁尝尝
烫个头体验一下,让自己更有自信
小汤3《双手交叉练习曲》打卡