Dynamic Programming21 sections · 916 units
Open in Course

From Two Dimensions to One

The transformation

Original DP: dp[i][k]dp[i][k] = best cost to partition positions 11 to ii into exactly kk segments. This is O(nK)O(n \cdot K) states. Penalized DP: dpλ[i]dp_{\lambda}[i] = best (cost + λ×\lambda \times count) to partition positions 11 to ii into any number of segments.

This is O(n)O(n) states. The penalized DP is much faster. You compute it for different λ\lambda values until you find the one that produces exactly KK segments.