题解 | #牛群买卖策略优化#
牛群买卖策略优化
https://www.nowcoder.com/practice/c8514318443a48218efde630ae11b4c3
import java.util.*;
public class Solution {
/**
* 理解:求价格数组中紧邻的递增差值的总和中最大的;
* 想象价格数组为一个折线图,求出折线图中所有的上升段的和
* [7,1,4,3,2,5]
*
* @param prices int整型一维数组
* @return int整型
*/
public int max_profitv2 (int[] prices) {
// write code here
int len=prices.length;
int max=0;
int temp=0;
int last=0;
for(int i=0;i<len;i++){
temp=0;
last=prices[i];
for(int j=i+1;j<len;j++){
if(prices[j]>last){
temp+=prices[j]-last;
}
last=prices[j];
}
max=Math.max(max,temp);
}
return max;
}
}
