Buying and Selling stocks with maximum profit
codingismycraft.com
codingismycraft.com
You can get a O(n) solution by just looking ahead to the next price and buy at all the local minima and sell at all the local maxima.
Edit: I see now that it says "two no[sic] overlapping" trade pairs, which invalidates my solution.
>[7, 3, 13, 13, 6, 19, 10, 8, 15, 18]
>the maximum profit that can be made is 26 which is made as
>follows:
>Buy at 3 – Sell at 19, Buy at 8 – Sell at 18
However wouldn't profit be maximized with
B3, S13, B6, S19, B8, S18 for a total of profit of 33?
There is a small typo which I think caused me some trouble parsing the sentence. In hindsight however the problem definition seems clear.