I am wondering about the correctness of this method:
We are given a function / algorithm findMaxProfit()
that can find the maximum profit when given an array of stock prices, and buy and sell once, with time complexity O(n)
, additional space complexity O(1)
. The buy must occur before the sell (meaning no short sell is allowed).
findMaxProfit()
will return the maximum profit, and the buy and sell point (as indexes into the array). Example: [3200, 6, 22]
meaning max profit is 3200, and buy on day 6, sell on day 22. (day in the description is also 0 index based).
Any buy and sell can occur on the same day, meaning profit = 0.
Now, find the maximum profit if you are allowed to buy and sell at most twice, with the second buy occurring on or after the first sell, and with time complexity O(n)
, additional space complexity O(1)
.
I think one assumption is that we buy the same number of shares both times (let's say just 1 share). Otherwise, if the earned money from the first time can be used to buy stocks the second time, then it is not the dollar amount we are concerned with, but the percentage gain overall.
// this function just returns the max profit, no need to return the
// buy / sell point. In JavaScript ES6
function findMaxProfitBuySellTwice(arr) {
const [max1, a, b] = findMaxProfit(arr);
return max1 + Math.max(findMaxProfit(arr.slice(0, a),
findMaxProfit(arr.slice(b + 1));
}
the a
can be a + 1
, while the b + 1
can be b
, but with no use. The reason is let's say if you buy on day 6, then from day 5 to day 6, there will be no profit, or else the findMaxProfit()
would have included day 5. Likewise, if findMaxProfit()
says to sell on day 22, then from day 22 to 23, there is no profit, so we really don't need to start at day 22 but start at day 23. slice()
will return empty array if it is out of bound.
About the correctness of the code, because the Maximum Stock Profit Problem is interchangeable with the Maximum Subarray Problem. In fact, the number returned by the Maximum Stock Profit Problem and the number returned by the Maximum Subarray Problem should actually be exactly the same number.
So if we are asked to find the 2 subarrays that will add up to maximum, we could do the same as above: (1) find the maximum subarray first. (2) now slide this region out, and find the max for the remaining two regions. (3) just add up the max from step (1) and the maximum of the 2 numbers from step (2).
It seems the correctness of doing this to the Maximum Subarray Problem is more obvious: the 2 maximum subarrays are just finding the max first, and then disregard this region, and find the max of the remaining 2 sets of data.
Note that the Maximum Stock Profit Problem and the Maximum Subarray Problem should be able to convert to each other, say, from the stock problem to the subarray problem by taking the difference between the daily prices (resulting an array with size 1 less in general):
function convertStockDataToMaxSubarrayData(arr) {
var maxSubarrayData = [], i;
if (arr.length <= 1) return [];
for (i = 1; i < arr.length; i++) {
maxSubarrayData.push(arr[i] - arr[i-1]);
}
return maxSubarrayData;
}
O(n)
solution for two trades. I'd love to be proven wrong, but I don't see how you'd do it. \$\endgroup\$