[Algorithms] Dynamic Programming - Theoretical Basis

Title Dynamic Programming Frequently Asked Questions

Basics of dynamic programming (Fibonacci sequence, climbing stairs), knapsack problem, house robbery, stock problem, subsequence problem

Five parts of dynamic programming

  1. In the process of state transfer, the meaning of the dp array and the following table;
  2. Find the recursive formula;
  3. How to initialize the dp array;
  4. traversal order;
  5. print the dp array (useful for checking your content)

There are other situations to consider:
"Rolling array thinking" is a common dynamic programming optimization method, which has been used many times in our topics, such as "Sword Pointing to Offer 46. Translating numbers into strings", "70. When the state we define is only related to a few states in the transition equation of dynamic programming, we can consider this optimization method, the purpose of which is to "reduce the dimensionality" of the space complexity. If you don't know what "rolling array thinking" is, be sure to consult relevant information to learn.

insert image description here

Dynamic programming definition and distinction

动态规划,英⽂:Dynamic Programming,简称DP,如果某⼀问题有很多重叠⼦问题,使⽤动态规划是最有效的。
所以动态规划中每⼀个状态⼀定是由上⼀个状态推导出来的,这⼀点就区分于贪⼼,贪⼼没有状态推导,⽽是从局
部直接选最优的,
在关于贪⼼算法,你该了解这些!中我举了⼀个背包问题的例⼦。
例如:有N件物品和⼀个最多能背重量为W 的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。每件物
品只能⽤⼀次,求解将哪些物品装⼊背包⾥物品价值总和最⼤。
动态规划中dp[j]是由dp[j-weight[i]]推导出来的,然后取max(dp[j], dp[j - weight[i]] + value[i])。
但如果是贪⼼呢,每次拿物品选⼀个最⼤的或者最⼩的就完事了,和上⼀个状态没有关系。
所以贪⼼解决不了动态规划的问题。
其实⼤家也不⽤死扣动规和贪⼼的理论区别,后⾯做做题⽬⾃然就知道了。
⽽且很多讲解动态规划的⽂章都会讲最优⼦结构啊和重叠⼦问题啊这些,这些东⻄都是教科书的上定义,晦涩难懂
⽽且不实⽤。
⼤家知道动规是由前⼀个状态推导出来的,⽽贪⼼是局部直接选最优的,对于刷题来说就够⽤了。

Guess you like

Origin blog.csdn.net/qq_40500099/article/details/132562799