Quant Memo
Coding/●●●●

At most k trades on one price path

Asked at Citadel, HRT

You may complete at most kk 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 O(nk)O(nk), and handle large kk specially.

Show a hint

Two states per allowed trade: "holding after the jj-th buy" and "done after the jj-th sell." If kk is at least n/2n/2 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.

More Coding questions