Dynamic Programming21 sections · 915 units
Open in Course

Sonya - DP Formulation

Convex function

dp[i]dp[i] as a function of the ii-th value is convex and piecewise linear. Initially, dp[0](v)=∣v−b0∣dp[0](v) = |v - b_0|, a V-shape centered at b0b_0. Adding ∣v−bi∣|v - b_i| preserves convexity. The constraint "v≥previous valuev \geq \text{previous value}" truncates the function on the left, keeping only where vv is at least the previous minimum.

After each step, the minimum of dp[i]dp[i] is at some breakpoint. You track breakpoints with a max-heap. The heap top is the best last value.