Algorithms and Data Structures for Programming Contests

Dynamic Programming (Cont'd)

Regroup (1)

High-level solution?

Is it recursive?
Yes: solution either does not use last block (fewer blocks, same target height) or it uses last block (fewer blocks, reduced target height).
Does it have optimal substructure?
Yes: if optimal solution does not use last block, then it is optimal for rest of blocks;
if optimal solution uses last block, then it is optimal for rest of blocks and reduced height.
Are subproblems "simple"?
Yes: each subproblem characterized by target height (range [0..H]) and index of last block available (range [0..S]).
Do subproblems overlap?
Yes: small subproblems (e.g., with S = 1) must be evaluated multiple times as part of solving larger problems.

Step 1: define a table of solutions.

Step 2: write recursive equations for table values.

Step 3: write the algorithm.

Step 4: reconstruct the solution.

Try it! (2)

Regroup


© Copyright 2012–2015 François Pitt <francois.pitt@utoronto.ca>
last updated at 08:24 (EST) on Thu 5 Mar 2015
Creative Commons License Algorithms and Data Structures by François Pitt is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.