一、思路
根据首次买入股票的值,与后一个数据做差值,及得到其利润。
二、输出结果:7
三、代码实现
def maxProfit(prices):
#最开始最大利润为0,许哟啊进行初始化
maxPro = 0
i = 1#若根据计算利润公式决定该值
while i < len(prices):
#计算后一个值比前一个值的利润大小,负数表示负利润
profit = prices[i] - prices[i-1]
if profit > 0:
maxPro += profit
i += 1
return maxPro
if __name__ == "__main__":
prices = [2, 1, 5, 3, 6, 4]
print(maxProfit(prices))