Dynamic Programming21 sections · 916 units
Open in Course

Heap Representation - Walkthrough

Storing breakpoints efficiently

A piecewise linear function is defined by its slope changes. If slope goes from 00 to 11 at x=5x = 5, we store 55. observation: we only need the breakpoints, not the full function.

The slopes between breakpoints are implicit. Two heaps: LL (max-heap) for left breakpoints, RR (min-heap) for right breakpoints. The minimum is in the 'valley'. To add xa|x - a|: insert aa into both LL and RR. Then balance: if L.top>R.topL.top > R.top, swap and adjust the base value.