Dynamic Programming21 sections · 916 units
Open in Course

Implementation Template

Reusable structure

Class with: maxHeap LL (left breakpoints), minHeap RR (right breakpoints), base value, optional offsets. Method addAbsValue(a): push aa to both heaps, balance if L.top>R.topL.top > R.top, update base. Method prefixMin(): clear right heap, base stays. Method query(): base value is the minimum, L.topL.top and R.topR.top give the interval of minimizers. Use a max-heap for left breakpoints and min-heap for right. The heaps maintain the piecewise linear function.

Time complexity: O(nlogn)O(n \log n) since each of the nn operations involves heap pushes and pops.

Space complexity: O(n)O(n) for the two heaps storing breakpoints.