3
#yyds干货盘点# 名企真题专题:最大差值
source link: https://blog.51cto.com/u_15488507/5919911
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
#yyds干货盘点# 名企真题专题:最大差值
精选 原创1.简述:
描述有一个长为 n 的数组 A ,求满足 0 ≤ a ≤ b < n 的 A[b] - A[a] 的最大值。
给定数组 A 及它的大小 n ,请返回最大差值。
数据范围: ,数组中的值满足
示例1[5,1],2
[5,6],2
2.代码实现:
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param A int整型一维数组
* @param n int整型
* @return int整型
*/
public int getDis (int[] A, int n) {
int maxNow = 0, maxSoFar = 0;
for (int i = 1;i < n;i++) {
maxNow = Math.max(0, maxNow + A[i] - A[i - 1]);
maxSoFar = Math.max(maxSoFar, maxNow);
}
return maxSoFar;
}
}
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param A int整型一维数组
* @param n int整型
* @return int整型
*/
public int getDis (int[] A, int n) {
int maxNow = 0, maxSoFar = 0;
for (int i = 1;i < n;i++) {
maxNow = Math.max(0, maxNow + A[i] - A[i - 1]);
maxSoFar = Math.max(maxSoFar, maxNow);
}
return maxSoFar;
}
}
- 赞
- 收藏
- 评论
- 分享
- 举报
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK