题解 | #买卖股票的最好时机(一)# Python3
买卖股票的最好时机(一)
https://www.nowcoder.com/practice/351b87e53d0d44928f4de9b6217d36bb
import sys # dp[i]为第i天卖出可获得的最高收益 # dp[i] = prices[i] - pre_min (之前天里面的最低值) # 最后输出max[dp] n = int(input()) if n<=1: print(0) else: dp = [0] * (n+1) dp[0] = 10**5 + 5 pre_min = dp[0] prices = list(map(int,input().strip().split(' '))) for i in range(1,n+1): dp[i] = prices[i-1] - pre_min pre_min = min(pre_min, prices[i-1]) max_profit = max(dp[1:]) print(max_profit if max_profit >0 else 0)