题解 | #最大差值#
最大差值
https://www.nowcoder.com/practice/a01abbdc52ba4d5f8777fb5dae91b204
开始想到动态规划,但是题目要求差值是前一个减后一个,第i个值最小时,不可作为减数。 所以 前i个数最大差值为 max(A[i]-min_,max_),每次更新A[:i]最小值min_; class Solution: def getDis(self , A: list[int], n: int) -> int: # write code here # if n <=1: # return 0 # dp=[0 for i in range(n)] # for i in range(1,n): # x = A[i] # max_ = max(A[:i]) # min_ = min(A[:i]) # if x < min_ : # dp[i] = dp[i-1] +(min_ - x) # elif x > max_ : # dp[i] = dp[i-1] + (x-max_) # else: # dp[i] = dp[i-1] # return dp[n-1] min_ = A[0] max_ = 0 for i in range(n): min_ = min(min_,A[i]) max_ = max(max_,A[i] - min_) return max_#我的实习求职记录#