我正在尝试为以下目的编写程序:
我当前的尝试是:https://imgur.com/a/k3jBzKR
def maxProfitWithKTransactions(prices, k):
k_profits = [0]
max_profit = 0
for _ in range(k):
if len(prices) == 0:
break
buy_stock_day = prices.index(min(prices))
buy_stock = min(prices)
sell_stock = max(prices[buy_stock_day:])
max_profit += sell_stock - buy_stock
k_profits.append(max_profit)
prices = prices[:buy_stock_day]
return max(k_profits)
失败:[5,11,3,50,40,90],2
我明白了为什么,但是我无法修改为概括该过程而制作的算法。有人可以指导我的思考过程,而不是另辟way径吗?