题解 | #买卖股票的最好时机(三)#

买卖股票的最好时机(三)

http://www.nowcoder.com/practice/4892d3ff304a4880b7a89ba01f48daf9



public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     * 两次交易所能获得的最大收益
     * @param prices int整型一维数组 股票每一天的价格
     * @return int整型
     */
    public int maxProfit (int[] prices) {
        if(prices.length == 0) return 0;
        int first_hold=-prices[0],second_hold=-prices[0];
        int first_sell=0,second_sell=0;
        for(int i=1;i<prices.length;i++){
             first_hold = Math.max(first_hold,-prices[i]);
             first_sell = Math.max(first_sell,first_hold+prices[i]);
             
            second_hold = Math.max(second_hold,first_sell-prices[i]);
            second_sell = Math.max(second_sell,second_hold + prices[i]);
         }
        return second_sell;
    }
}
全部评论

相关推荐

运营你豪哥:简历改改吧-非本、求职意向技术岗、无实习经历、内容空洞 如果简历不爆改的话,应该是会持续崩溃了 1.把你教育经历放最下面去 2.蓝底照片很奇怪哈,感觉还在高中时代,建议白底重新拍一下 3.校园经历没啥必要,收集和反馈同学们对产品的意见,解决学生和老师之间的沟通,企业招聘不看这些哈 好好思考一下简历的设计和你要表达的重点,再去投简历
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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