Two trades with a per-trade commission
Asked at DE Shaw, Jump Trading
You may complete at most two buy-then-sell trades on a price path, and the second buy cannot happen before the first sell. Every time you complete a trade (a sell), the broker charges a fixed fee.
prices = [1, 5, 3, 9], fee = 1
-> 8 # trade 1: buy 1, sell 5 -> +4 - 1 = 3
# trade 2: buy 3, sell 9 -> +6 - 1 = 5
Return the maximum net profit after fees. Aim for with extra space.
Show a hint
The fee only lands when you sell. Track four running states: holding after the first buy, done with the first sell, holding after the second buy, done with the second sell. Subtract the fee at each sell.
Your answer
This one is open-ended. Work it through, then check your reasoning against the full solution.