r/codinginterview • • May 15 '20

DP == Optimization of repeated sub problems.

How to identify every DP problem.

  1. Check if greedy (Choosing an optimal decision at every step) works for the problems. If not

  2. Draw the recursive brute force solution of a problem and Identify the repeated sub problem.

  3. Decide whether to use a 1D array or a 2D to memoize the repeated sub problems results.

Every DP problem can be identified using above cardinal rules. DP problems are easiest to decipher.

2 Upvotes

2 comments sorted by

2

u/bparanj May 15 '20

Here is my notes from MIT course video:

5 Easy Steps to DP

  1. Define subproblems

  2. Guess (part of the solution)

  3. Relate subproblem solutions

  4. Recurse and memoize or build DP table bottom-up

  5. Solve original problem.