DP 和贪心都需要最优子结构。区别是:贪心还需要贪心选择性质(局部最优就是全局最优),而DP 是在你必须考虑多个选择和重叠子问题时需要的。
区别
text
Optimal substructure? -- both need this
+ greedy-choice property holds? -> GREEDY (fast, one pass of choices)
+ must compare many sub-solutions / they overlap? -> DYNAMIC PROGRAMMING
经典对比:硬币兑换
python
():
INF = ()
dp = [] + [INF] * amount
a (, amount + ):
c coins:
c <= a:
dp[a] = (dp[a], dp[a - c] + )
dp[amount]
min_coins(, [, , ])
