Consider the array of tuples $(t_i, p_i, q_i)$, where $t_i \in \{0, 1\}$ means type of order ($0$ for sell and $1$ for buy), $p_i$ is price of order and $q_i \in (0, 1)$ is volume of the order. Assume we know all $\left[(t_1, p_1, q_1) \ldots (t_n, p_n, q_n)\right]$. We want to construct an algorithm to maximize profit, assuming the following constraints: we can use only deals from the given list and our position (amount of volume we have) is bounded by $1$. Fo example: if we have a sequence of two buy orders with $p = 1$ and $q = 0.5$, and sell order after with $p > 1$, then we receive some positive profit.
My question: obviously, since we have a list of all data, we can start from the end and make a greedy algorithm (buy till possible, then sell with best price and vice versa). But I guess it's not so optimal and the task assumes dynamic programming.
The easiest dynamic possible (I guess) is $d(i)$ is maximum profit based on first $i$ orders. But I unsure about correct dynamic step here. Any ideas?