您的位置:首页 > 其它

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

2018-01-17 13:33 399 查看

描述

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);
}
};


致谢

此篇博客中的算法由林翰老师提出

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: