Quant Memo
Coding/●●●●●

Best uptick with a minimum holding gap

You may buy at one tick and sell at a later tick, but your strategy forces you to hold for at least d ticks, the sell index must be at least d after the buy index. Maximize sell_price - buy_price under that constraint. The answer may be negative if every legal pair loses money.

prices = [5, 3, 6, 8, 2], d = 2
-> 5            # buy at index 1 (3), sell at index 3 (8)

Return the maximum legal prices[j] - prices[i] with j - i >= d. Aim for O(n)O(n).

Your answer

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

More Coding questions