Dynamic Programming21 sections · 916 units
Open in Course

Querying the Minimum

Finding the best line

To query at xix_i:

1.1. While the deque has 2\geq 2 lines, check if the second line is better than the first at xix_i.

2.2. If L2(xi)<L1(xi)L_2(x_i) < L_1(x_i), pop the front. The first line will never be optimal again because queries are increasing.

3.3. Repeat until the front is optimal.

4.4. Return L1(xi)L_1(x_i). Because queries are sorted, once you pop a line, you never need it again. Each line leaves the deque at most once.