Common problems and usage

Dynamic programming is similar to the divide-and-conquer method, solving problems by combining the solutions to subproblems. But the main difference is that dynamic programming doesn’t solve the common subproblem repeatedly. A DP algorithm solves subproblems just once and then saves its answer in a table, thereby avoiding the work of recomputing the answer every time it solves each subproblem.

How to develop a DP algorithm

  1. Characterize the structure of an optimal solution.
  2. Recursively define the value of an optimal solution.
  3. Compute the value of an optimal solution, typically in a bottom-up fashion.
  4. Construct an optimal solution from computed information.

Memoization and Tabulation

If we observe that a recursive solution is inefficient because it solves the same subproblems repeatedly, we arrange for each subproblem to be solved only once, saving its solution.

Top-down or Memoization

  1. Use a recursive approach similar to the non-DP solution.
  2. Add a memoization table (array or hash map) to store the results of subproblems.
  3. Before computing a subproblem, check if it's already solved in the memo table.

Bottom-up or Tabulation

  1. Create a table to store the maximum profit for each rod length from 1 to n.
  2. Fill the table iteratively, using smaller subproblems to solve larger ones.
  3. The final answer will be in the last cell of the table.

Example - Matrix Multiplication