LeetCode题解:123. Best Time to Buy and Sell Stock III

Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete at most two transactions.

Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

原题地址

上一篇文章写了交易一次能获得的最大利润。该题目要求买卖两次得到最大利润。按照上篇文章的思路,可以同样衍生出两种思路;

思路1:

本题是交易两次,可以分成两个一次交易的问题来解,如果我告诉你,第一次的交易是在哪一天结束的。那么这个问题就变成了两个一次交易的问题。可以用上一篇文章的思路1来解;可以枚举所有第一次交易完成的天数的可能。

当枚举到第k天的时候,会重复计算k-1天时候的数据。用数组来存储之前计算过的状态。第一次交易的计算从前向后计算,第二次交易的计算从后向前计算,因为这样才能利用之前计算的状态。

计算第一次交易时,minPrice存储当前最低的价格,firstTran[i]记录到第i天的时候,能获得的最大利润。

minPrice = min{minPrice,prices[i-1]}

firstTran[i] = max{firstTran[i-1],prices[i-1]-minPrice}

计算第二次交易时,maxPrice存储当前的最高价格,secondTran[i] 记录第i天到最后一天,能获得的最大利润。

maxPrice = max{maxPrice,prices[i+1]}

secondTran[i] = max{secondTran[i+1],maxPrice-prices[i-1]}

最后枚举所有第一次交易完成的可能,求最大值。

class Solution {
    public int maxProfit(int[] prices) {
        if (prices == null || prices.length == 0) return 0;
        int[] firstTran = new int[prices.length + 2];
        int[] secondTran = new int[prices.length + 2];
        int minPrice = Integer.MAX_VALUE;
        for (int i = 1; i <= prices.length; i++) {
            minPrice = Integer.min(minPrice, prices[i - 1]);
            firstTran[i] = Integer.max(firstTran[i - 1], prices[i - 1] - minPrice);
        }
        int maxPrice = Integer.MIN_VALUE;
        for (int i = prices.length; i >= 1; i--) {
            maxPrice = Integer.max(maxPrice, prices[i - 1]);
            secondTran[i] = Integer.max(secondTran[i + 1], maxPrice - prices[i - 1]);
        }
        int maxAns = 0;
        for (int i = 1; i <= prices.length; i++) {
            maxAns = Integer.max(maxAns, firstTran[i] + secondTran[i]);
        }
        return maxAns;
    }
}

思路2:

按照上一篇博客中的写到,无非就是通过第一次的卖控制第一次的最大值,第二次的卖控制整体的最大值。
  1. public class Solution {  
  2.     public int maxProfit(int[] prices) {  
  3.         int buy = Integer.MIN_VALUE, sell = 0;  
  4.         for (int price : prices) {  
  5.             buy = Integer.max(buy, -price);  
  6.             sell = Integer.max(sell, buy + price);  
  7.         }  
  8.         return sell;  
  9.     }  

后面再加两行,返回最后的sell即可。

class Solution {
    public int maxProfit(int[] prices) {
        int buy1 = Integer.MIN_VALUE, buy2 = Integer.MIN_VALUE, sell1 = 0, sell2 = 0;
        for (int price : prices) {
            buy1 = Integer.max(buy1, -price);
            sell1 = Integer.max(sell1, buy1 + price);
            buy2 = Integer.max(buy2, sell1 - price);
            sell2 = Integer.max(sell2, buy2 + price);
        }
        return sell2;
    }
}
后面的这种思路更加通用一些,推荐后面的这种做法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值