Quant Memo
Coding/●●●●●

Single trade with a holding period

You may buy once and sell once, and the sell must come at least k days after the buy (a lockup). Short selling is not allowed; if no profitable trade meets the lockup, do nothing.

prices = [5, 3, 6, 8, 4], k = 2
-> 5           # buy at 3 (day 1), sell at 8 (day 3), gap of 2 days

prices = [9, 8, 7], k = 1
-> 0           # prices only fall

Return the maximum profit. Aim for O(n)O(n) time and O(1)O(1) space.

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions