Dynamic Programming21 sections · 916 units
Open in Course

Implementation Checklist

Avoiding common bugs

D&C checklist: pass opt_l and opt_r bounds, compute mid correctly, update opt[mid], recurse with updated bounds. Knuth checklist: iterate by increasing interval length, track opt[i][j], make sure opt[i][j-1] and opt[i+1][j] are computed first. Boundary conditions: what is opt for base cases? Usually opt[i][i] = i or similar. Off-by-one errors are common. Verification: print optimal splits for small inputs. Check they're monotonic. If not, QI doesn't hold for your cost function.

Time complexity: O(n2)O(n^2) with Knuth optimization, O(n2logn)O(n^2 \log n) with D&C optimization.

Space complexity: O(n2)O(n^2) for the DP and opt tables.