题解 | 买卖股票的最好时机(一)
买卖股票的最好时机(一)
https://www.nowcoder.com/practice/351b87e53d0d44928f4de9b6217d36bb
n = int(input())
arr = list(map(int, input().split()))
if n == 0:
print(0) # 如果数组为空,收益为 0
else:
min_price = arr[0] # 当前能够买入的最低价格
max_profit = 0 # 当前能够获得的最大收益
for i in range(1, n):
if arr[i] < min_price:
min_price = arr[i] # 更新最低价格
else:
max_profit = max(max_profit, arr[i] - min_price) # 更新最大收益
print(max_profit)
