//本题用动态规划求解
//定义一维数组dp【n】为 从开始到 以第n个元素为b的最大差值
//理解了dp的定义 , 记录最大值返回即可
import java.util.*;
public class Solution {
public int getDis (int[] A, int n) {
// write code here
int[] dp = new int[n];
dp[0] = 0;
int min = A[0];
int ans = 0;
for (int i = 1; i < n; i++) {
if (min > A[i-1]){
min = A[i-1];
}
dp[i] = A[i] - min;
ans = max(dp[i],ans);
}
return ans;
}
public static int max(int a, int b) {
return (a >= b) ? a : b;
}
}
//c++
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param A int整型vector
* @param n int整型
* @return int整型
*/
int getDis(std::vector<int>& A, int n) {
// write code here
int dp[n];
dp[0] = 0;
int min = A[0];
int res = 0;
for (int i = 1; i < n; ++i) {
if (A[i - 1] < min) {
min = A[i - 1];
}
dp[i] = A[i] - min;
res = res > dp[i] ? res : dp[i];
}
return res;
}
};
//python3
class Solution:
def getDis(self , A: List[int], n: int) -> int:
dp = [0] * n
min = A[0]
res = 0
count = 1
for i in A[1:] :
if min > i:
min = i
dp[count] = i -min
res = max(res,dp[count])
count +=1
return res