When to Use DP

When it comes to solving an algorithm problem, especially in a high-pressure scenario such as interview, half the battle is figuring out how to even approach the problem. In the first section, we define what makes a problem a good candidate for dynamic programming.

  1. The problem can be broken down into overlapping subproblems - smaller versions of the original problem that are re-used multiple times
  2. The problem has an optimal substructure - an optimal solution can be formed from optimal solutions to the overlapping subproblems of the original problem.

The first characteristic that is common in DP problems is that the problem will ask for the optimum value (maximum or minimum) of something, or the number of ways there are to do something. For example:

  • What is the minimum cost of doing…
  • What is the maximum profit from …
  • How many ways are there to do …
  • What is the longest possible …
  • Is it possible to reach a certain point …

When it comes to identifying if a problem should be solved with DP, this first characteristic is not sufficient. Sometimes, a problem in this format (asking for the max/min/longest etc.) is meant to be solved with a greedy algorithm. The next characteristic will help us determine whether a problem should be solved using a greedy algorithm or dynamic programming.

The second characteristic that is common in DP problems is that future “decisions” depend on earlier decisions. Deciding to do something at one step may affect the ability to do something in a later step. This characteristic is what makes a greedy algorithm invalid for a DP problem - we need to factor in results from previous decisions.


House Robber is an excellent example of a dynamic programming problem. The problem description is:

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact if two adjacent houses were broken into on the same night.
Give an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.

In this problem, each decision will affect what options are available to the robber in the future. For example, with the test case nums=[2,7,9,3,1], the optimal solution is to rob the houses with 2, 9, and 1 money. However, if we were to iterate from left to right in a greedy manner, our first decision would be whether to rob the first or second house. 7 is way more money than 2, so if we were greedy, we would choose to rob house 7. However, this prevents us from robbing the house with 9 money. As you can see, our decision between robbing the first or second house affects which options are available for future decisions.


When you are solving a problem on your own and trying to decide if the second characteristic is applicable, assume it isn’t, then try to think of a counterexample that proves a greedy algorithm won’t work. If you can think of an example where earlier decisions affect future decisions, then DP is applicable.

To summarize: if a problem is asking for the maximum/minimum/longest/shortest of something, the number of ways to do something, or if it is possible to reach a certain point, it is probably greedy or DP. With time and practice, it will become easier to identify which is better approach for a given problem. Although, in general, if the problem has constraints that cause decisions to affect other decisions, such as using one element prevents the usage of other elements, then we should consider using dynamic programming to solve the problem. These two characteristics can be used to identify if a problem should be solved with DP.

猜你喜欢

转载自blog.csdn.net/BSCHN123/article/details/121855420