I was given a problem at a job interview, I'm trying to solve it afterwards
You are given a list of N trades for some stock, you need to determine how much volume for each trade an ideal strategy would execute, provided that you cannot buy or short more than K (=const) shares. The algorithm should work for O(N) and be constant in memory.
It is clear how to solve such a problem by dynamic programming (with memory O(K) and complexity O(N * K) = O(N)), but the running time on 1e5 trades is about a couple of hours, although it is necessary to work for a couple of seconds. Maybe there are some optimisations for such dynamics?
(fyi. $d[i][p] = max(d[i-1][p],\,\,\,\, d[i-1][p - \text{side_sign} * x] - \text{side_sign} \cdot x \cdot \text{trade['price']}$ $x \in [1, \text{ trade['amount']}])$, $\text{side_sign = (-1 if trade['side'] == 'short' else 1)}$)