Dynamic Programming21 sections · 916 units
Open in Course

Adding a New Line

Maintaining the hull

When adding line LnewL_{new} with slope mnewm_{new}:

1.1. While the deque has 2\geq 2 lines, check if the last line is now dominated.

2.2. Let L1L_1 be second-to-last, L2L_2 be last. If L2L_2 loses to both L1L_1 and LnewL_{new} everywhere between their intersections, pop L2L_2.

3.3. Formally: if intersect(L1,Lnew)intersect(L1,L2)intersect(L_1, L_{new}) \leq intersect(L_1, L_2), then L2L_2 is useless. Pop it.

4.4. Push LnewL_{new}. This maintains the lower envelope invariant.