Dynamic Programming21 sections · 916 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)=vb0dp[0](v) = |v - b_0|, a V-shape centered at b0b_0. Adding vbi|v - b_i| preserves convexity. The constraint "vprevious 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.