题解 | #股票交易的最大收益(二)#

股票交易的最大收益(二)

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

import java.util.*;


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

相关推荐

03-09 20:32
运营
牛客972656413号:成绩管理系统会不会有点太。。。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务