当前位置:网站首页>动态规划总括
动态规划总括
2022-08-04 05:14:00 【empty__barrel】
如果某一问题有很多重叠子问题,使用动态规划是最有效的。
所以动态规划中每一个状态一定是由上一个状态推导出来的由此来判断是否使用动态规划
动规是由前一个状态推导出来的,而贪心是局部直接选最优的
动态规划中dp[j]是由dp[j-weight[i]]推导出来的,然后取max(dp[j], dp[j - weight[i]] + value[i])。
但如果是贪心呢,每次拿物品选一个最大的或者最小的就完事了,和上一个状态没有关系。
对于动态规划问题,我将拆解为如下五步曲:
- 确定dp数组(dp table)以及下标的含义
- 确定递推公式
- dp数组如何初始化
- 确定遍历顺序
- 举例推导dp数组
动态规划应该如何debug:
找问题的最好方式就是把dp数组打印出来,看看是不是和自己预先推导的哪里不一样。
如果打印出来和自己预先模拟推导是一样的,那么就是自己的递归公式、初始化或者遍历顺序有问题了。
如果和自己预先模拟推导的不一样,那么就是代码实现细节有问题。
解题:
首先思考这是一个动态规划的题目,那么当前值是由前面的值表示出来的,怎么表示呢?不清楚。此时就应该通过答案反推过程同时在此过程中要始终记得当前值是可以由前面的值表示的。
dp[ n ] = 某某+dp[ i ]
dp[ i] = 某某+dp[ i] +/-/*/÷ dp[ j ]
边栏推荐
- 应届生软件测试薪资大概多少?
- 深度学习21天——准备(环境配置)
- [Evaluation model] Topsis method (pros and cons distance method)
- The difference between px, em, and rem
- Programming hodgepodge (4)
- [Skill] Using Sentinel to achieve priority processing of requests
- C Expert Programming Chapter 5 Thinking about Linking 5.2 Advantages of Dynamic Linking
- 【21天学习挑战赛】顺序查找
- There is an 8 hour difference between the docker installation of mysql and the host.
- 3000字,一文带你搞懂机器学习!
猜你喜欢

败给“MySQL”的第60天,我重振旗鼓,四面拿下蚂蚁金服offer

Turn: Management is the love of possibility, and managers must have the courage to break into the unknown
![[One step in place] Jenkins installation, deployment, startup (complete tutorial)](/img/f2/15fb546eb864d7ff40b5507d5c7aa5.png)
[One step in place] Jenkins installation, deployment, startup (complete tutorial)

Simple operation of the file system
![[21 Days Learning Challenge] Image rotation problem (two-dimensional array)](/img/51/fb78f36c71e1eaac665ce9f1ce04ea.png)
[21 Days Learning Challenge] Image rotation problem (two-dimensional array)

SLSA 框架与软件供应链安全防护
![[Cloud Native--Kubernetes] Pod Resource Management and Probe Detection](/img/1a/b3bdf9b62c82b0fc4d913045981d94.png)
[Cloud Native--Kubernetes] Pod Resource Management and Probe Detection

解决错误:npm WARN config global `--global`, `--local` are deprecated

idea设置识别.sql文件类型以及其他文件类型

The idea setting recognizes the .sql file type and other file types
随机推荐
C专家编程 第4章 令人震惊的事实:数组和指针并不相同 4.1 数组并非指针
sql server如何得到本条记录与上一条记录的差异,即变动值
Get the selected content of the radio box
day13--postman接口测试
QT 如何识别文件的编码格式
day13--postman interface test
Explain detailed explanation and practice
[Evaluation model] Topsis method (pros and cons distance method)
Programming hodgepodge (4)
word 公式编辑器 键入技巧 | 写数学作业必备速查表
el-Select selector bottom fixed
How to keep the source code confidential in the development under the burning scenario
深度学习环境配置
docker安装mysql与宿主机相差8小时的问题。
The idea setting recognizes the .sql file type and other file types
Cache pool of unity framework
注意!软件供应链安全挑战持续升级
【21天学习挑战赛】顺序查找
C专家编程 第5章 对链接的思考 5.1 函数库、链接和载入
【21天学习挑战赛】直接插入排序