Algorithms and Data Structures for Programming Contests

Dynamic Programming (Cont'd)

Regroup (2)

High-level solution?

Is it recursive?
Yes: solution either does not use last song or it does (in which case capacity is reduced accordingly).
Does it have optimal substructure?
Yes: if optimal solution does not use last song, then it is optimal (highest total rating) for rest of songs and original capacity;
if optimal solution uses last song, then it is optimal for rest of songs and reduced capacity.
Are subproblems "simple"?
Yes: each subproblem characterized by capacity (range [0..C]) and index of last song considered (range [0..N]).
Do subproblems overlap?
Yes: small subproblems (e.g., with N = 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.

Further examples

To learn more...

Back to Algorithms


© 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.