leetcode刷題(30天)-16. 最接近的三23. 買賣股票的最佳時機 III

class Solution {
    public int maxProfit(int[] prices) {
    if (prices.length == 0) {
        return 0;
    } 
    int s1 = -prices[0];
    int s2 =Integer.MIN_VALUE;
    int s3 =Integer.MIN_VALUE;

    int s4 = Integer.MIN_VALUE;

    for (int i=1;i<prices.length;i++){
        s1 = Math.max(s1, -prices[i]);
        s2 = Math.max(s2, s1+prices[i]);  // 賣出當前股票
        s3 = Math.max(s3, s2-prices[i]);  // 第二次購買當前股票
        s4 = Math.max(s4, s3+prices[i]);
    }
     return Math.max(0,s4);
    
}

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章