LeetCode题解系列--123. Best Time to Buy and Sell Stock III

16 篇文章 0 订阅

描述

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).

思路

此题是121和122的续集,我这里使用了一个比较容易理解的方法,虽然空间开销比较大,但是其实很容易优化成空间复杂度O(1)的方法,不过为了容易理解,先讲述空间开销比较大的方法。
算法如下:
我的算法需要新开四个数组:
第一个数组:表示第i天前的股票最低价格

// min price before day i, item 0 is useless
 vector<int> minPricesBeforeDayI(n, 0);
        minPricesBeforeDayI[0] = prices[0];
        for (int i = 1; i < n; ++i) {
            minPricesBeforeDayI[i] = min(minPricesBeforeDayI[i - 1], prices[i - 1]);
        }

第二个数组:表示前i天通过一次交易可以获得最大收益

// maximus profit gained by one transactions before day i
        vector<int> maxProfitBeforeDayI(n, 0);
        for (int i = 1; i < n; ++i) {
            maxProfitBeforeDayI[i] = max(maxProfitSellOnDayI[i - 1], prices[i] - minPricesBeforeDayI[i]);
        }

第三个数组:表示第i天后股票的最大价格

 // maximum price after day i, item (n-1) is useless
        vector<int> maxPriceAfterDayI(n, 0);
        for (int i = n - 2; i > -1; --i) {
            maxPriceAfterDayI[i] = max(maxPriceAfterDayI[i + 1], prices[i + 1]);
        }

第四个数组:表示第i天后(包括第i天)通过一次交易可以获得最大的收益

// maximum profit can be gained by one transaction after day i, item (n-1) is useless
        vector<int> maxProfitAfterDayI(n, 0);
        for (int i = 0; i < n - 1; ++i) {
            maxProfitAfterDayI[i] = max(maxProfitAfterDayI[i + 1], maxPriceAfterDayI[i] - prices[i]);
        }

最后,找到一个i满足前i天的收益和第i天后的收益相加最大,即为所求:

int resultTwoTransactions = 0;
        for (int i = 1; i < n; ++i) {
            resultTwoTransactions = max(resultTwoTransactions, maxProfitBeforeDayI[i] + maxProfitAfterDayI[i]);
        }

同时,我们需要考虑一次交易时获得最大的收益:(因为题目要求是最多两次交易)

int resultOneTransactions = 0;
        for (int i = 1; i < n; ++i) {
            resultOneTransactions = max(resultOneTransactions, maxProfitBeforeDayI[i]);
        }

最后,两者的最大值即为答案:

return max(resultOneTransactions, resultTwoTransactions);

完整解答


class Solution {
public:
    int maxProfit(vector<int>& prices) {
        // for weird input
        if (prices.size() <= 1) {
            return 0;
        }
        int n = prices.size();
        // min price before day i, item 0 is useless
        vector<int> minPricesBeforeDayI(n, 0);
        minPricesBeforeDayI[0] = prices[0];
        for (int i = 1; i < n; ++i) {
            minPricesBeforeDayI[i] = min(minPricesBeforeDayI[i - 1], prices[i - 1]);
        }
        // maximus profit gained by one transactions before day i
        vector<int> maxProfitBeforeDayI(n, 0);
        for (int i = 1; i < n; ++i) {
            maxProfitBeforeDayI[i] = max(maxProfitSellOnDayI[i - 1], prices[i] - minPricesBeforeDayI[i]);
        }
        // maximum price after day i, item (n-1) is useless
        vector<int> maxPriceAfterDayI(n, 0);
        for (int i = n - 2; i > -1; --i) {
            maxPriceAfterDayI[i] = max(maxPriceAfterDayI[i + 1], prices[i + 1]);
        }
        // maximum profit can be gained by one transaction after day i, item (n-1) is useless
        vector<int> maxProfitAfterDayI(n, 0);
        for (int i = 0; i < n - 1; ++i) {
            maxProfitAfterDayI[i] = max(maxProfitAfterDayI[i + 1], maxPriceAfterDayI[i] - prices[i]);
        }
        int resultTwoTransactions = 0;
        for (int i = 1; i < n; ++i) {
            resultTwoTransactions = max(resultTwoTransactions, maxProfitBeforeDayI[i] + maxProfitAfterDayI[i]);
        }
        int resultOneTransactions = 0;
        for (int i = 1; i < n; ++i) {
            resultOneTransactions = max(resultOneTransactions, maxProfitBeforeDayI[i]);
        }
        return max(resultOneTransactions, resultTwoTransactions);
    }
};

致谢

此篇博客中的算法由林翰老师提出
这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值