Version 1.5.1 shows the following solution which is indicated to have O(n) time complexity and O(n) additional space complexity:
int BuyAndSellStockTwice() { double max_total_profit = 0; vector<double> first_buy_sell_profits(prices.size(), 0); double min_price_so_far = numeric_limits<double>::max(); //for each day record max profit if sell made that day for (size_t i = 0; i < prices.size(); ++i) { min_price_so_far = min(min_price_so_far, prices[i]); max_total_profit = max(max_total_profit, prices[i] - min_price_so_far); first_buy_sell_profits[i] = max_total_profit; } //for each day find max profit if second buy made that day double max_price_so_far = numeric_limits<double>::min(); for (size_t i = prices.size() - 1; i > 0; --i) { max_price_so_far = max(max_price_so_far, prices[i]); max_total_profit = max(max_total_profit, max_price_so_far - prices[i] + first_buy_sell_profits[i - 1]); } return max_total_profit; }
I propose the following alternative, also with O(n) time complexity and with only O(1) additional space complexity (an improvement to the O(n) space complexity of the above current solution):
double BuyAndSellStockTwice(const vector<double>& prices) {
double max1 = 0, max2 = 0;
double min_price_so_far = numeric_limits<double>::max();
for(auto const& cur : prices) {
if (min_price_so_far > cur) min_price_so_far = cur;
double profit = cur - min_price_so_far;
if (profit > max2) {
if (profit > max1) {
max2 = max1;
max1 = profit;
}
else {
max2 = profit;
}
}
}
double max_total_profit = max1 + max2;
return max_total_profit;
}
Cheers.