Overview
Used for problems where we partition an array into K segments minimizing cost. Normal DP takes O(N^2) per segment. We can reduce this if the cost function satisfies the quadrilateral inequality.
Loading...
Loading Curriculum...
Loading Subject...
Loading Topic...
Loading Lesson...
Loading Lab...
Optimize DP from O(K * N^2) to O(K * N log N) when the optimal split point `opt(i, j)` satisfies monotonicity: `opt(i, j) <= opt(i, j+1)`.
Used for problems where we partition an array into K segments minimizing cost. Normal DP takes O(N^2) per segment. We can reduce this if the cost function satisfies the quadrilateral inequality.
If the optimal split point `opt(i, j)` for segment length `j` moves to the right as `j` increases, we know that `opt(i, j-1) <= opt(i, j) <= opt(i, j+1)`.
Instead of iterating `j` sequentially, we compute it for `mid`. The found `opt(mid)` restricts the search space for the left half `[left, mid-1]` to `[optLeft, optMid]` and the right half to `[optMid, optRight]`.
Because the search space is halved at each recursion depth, the total work per level is O(N). The recursion depth is O(log N), giving O(N log N) per row.