When to Use DP

本文探讨了如何确定算法问题是否适合使用动态规划(DP)来解决。DP问题通常涉及寻找最优解,且问题的子问题重叠,并依赖于先前的决策。例如,房屋盗贼问题展示了DP的适用性,因为当前的决策会影响未来的选项。识别问题是否适合DP的关键在于,未来决策是否受到早期决策的影响。如果存在这样的依赖,那么DP可能是解决问题的有效方法。
摘要由CSDN通过智能技术生成

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.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值