Skip to content

Dynamic Programming

A problem-solving technique to solve complex problems by breaking them down into smaller, overlapping subproblems, solving each subproblem only once.

Each algorithm defines a state, transition, and base case to compute the solution.

Dynamic programming is recursion without repetition. The proper recursion is much important than memoization.

Whenever you write - or even think - the word "greeDY", your subconscious is telling you to use DYnamic programming.

How to Develop a DP algorithm

  1. Formulate the problem recursively
  2. Specification: Describe the problem rather how to solve, but what the problem

  3. Solution, give a formula for the problem in terms of the answers to smaller instances of exactly the same problem

  4. Build solutions to your recurrence from the bottom up

  5. identify the subproblems

  6. choose a memoization data structure
  7. identify dependency
  8. Find a good evalution order
  9. analyze space and running time
  10. write down the algoritm

Mechanical Recipe

  • Subproblems
  • Memoization Strucure
  • Dependencies
  • Evaluation Order
  • Space and time

What DP mean

The programming is not code, but rather the older sense of planning or schedualing, typically by filling in a table.

动态规划步骤

  1. Define the DP state

  2. Formulate the recurrence relation

  3. Iterate the DP values

  4. Handle Edge Cases

  5. Return