At most k trades on one price path
Asked at Citadel, HRT
You may complete at most buy-then-sell trades on a price path, never holding two positions at once.
prices = [3, 2, 6, 5, 0, 3], k = 2
-> 7 # buy 2 sell 6 -> +4, buy 0 sell 3 -> +3
Return the maximum total profit. Aim for , and handle large specially.
Show a hint
Two states per allowed trade: "holding after the -th buy" and "done after the -th sell." If is at least you can take every upswing, so the cap stops binding.
Your answer
This one is open-ended. Work it through, then check your reasoning against the full solution.