Dynamic Programming21 sections · 916 units
Open in Course

Printing Neatly - Problem Statement

Line breaking optimization

Given nn words with lengths lil_i and line width WW. find the lowest sum of (trailing space)^3 for all lines except the last. dp[i]dp[i] = min cost to arrange words ii to nn.

Transition: dp[i]=minj>i(dp[j]+cost(i,j1))dp[i] = \min_{j > i} (dp[j] + cost(i, j-1)) where costcost is cube of spaces. The cube cost penalizes uneven lines heavily. QI can be shown to hold for this cost. Apply D&C improvement: O(nlogn)O(n \log n) instead of O(n2)O(n^2). This is it behind TeX's line breaking.