首页 > 其他 > 详细

leetcode——123. 买卖股票的最佳时机 III

时间:2020-06-25 18:37:18      阅读:48      评论:0      收藏:0      [点我收藏+]
public int maxProfit(int[] prices) {
        int n = prices.length;
        if(n<2){
            return 0;
        }
        int[] f = new int[n];
        int[] g = new int[n];
        for(int i = 1,valley = prices[0];i<n;i++){
            valley = Math.min(valley,prices[i]);
            f[i] = Math.max(f[i-1],prices[i] - valley);
        }
        for(int i = n-2,peak = prices[n-1];i>=0;i--){
            peak = Math.max(peak,prices[i]);
            g[i] = Math.max(g[i],peak-prices[i]);
        }

        int max_profit = 0;
        for(int i = 0;i<n;i++){
            max_profit = Math.max(max_profit,f[i]+g[i]);
        }
        return max_profit;
    }

技术分享图片

 

 

思路啊思路啊思路啊思路啊

设置valley这点以及peak这点我没能想到……

难过哦。

——2020.6.25

leetcode——123. 买卖股票的最佳时机 III

原文:https://www.cnblogs.com/taoyuxin/p/13192348.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!