股票最大利润
input: 价格序列 [1, 3, 2, 8, 6]
output: 7
思路:
找到第n天前最小价格
计算第n天的最大利润
maxpro = price[i] - min_price
price = [1, 3, 2, 8, 6]
def max_profit(arr):
if len(arr) <= 1:
return 0
max_pro = 0
min_pri = arr[0]
for i in arr[1:]:
min_pri = min(min_pri, i)
max_pro = max(max_pro, i-min_pri)
return max_pro
max_profit(price)
7