题解 | #牛群买卖策略优化#

牛群买卖策略优化

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;

    }
}

全部评论

相关推荐

求面试求offer啊啊啊啊:把华北改为华南再试一试,应该就没啥问题了。改完可能都不用投,别人主动联系了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务