度小满,股票买入卖出变种题。
最大化收益,最小化买卖次数。
def stock():
profit = 0
length = len(trade)
count = 0
for i in range(length):
temp = trade[i] - trade[i - 1]
if temp > 0:
profit += temp
if i + 1 < length and trade[i + 1] >= trade[i]: # 如果数组还在递增,那么不要卖出。
pass
else:
if temp > 0:
count += 1 # 最后乘以2是因为,一个交易的买入卖出,算2次操作。
print(profit, count * 2)
stock()