当前位置:网站首页>动态规划总括
动态规划总括
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 ]
边栏推荐
- 读者让我总结一波 redis 面试题,现在肝出来了
- 结构体指针知识要点总结
- Dynamic programming of the division of numbers
- 关于yolo7和gpu
- share总结
- 获取单选框选中内容
- Performance testing with Loadrunner
- 力扣题解8/3
- The idea setting recognizes the .sql file type and other file types
- C Expert Programming Chapter 4 The Shocking Fact: Arrays and pointers are not the same 4.4 Matching declarations to definitions
猜你喜欢
Do you think border-radius is just rounded corners?【Various angles】
[21 Days Learning Challenge] Image rotation problem (two-dimensional array)
Explain detailed explanation and practice
System design. Seckill system
【评价类模型】Topsis法(优劣解距离法)
docker安装mysql与宿主机相差8小时的问题。
关于yolo7和gpu
编程大杂烩(三)
编程大杂烩(四)
Turn: Management is the love of possibility, and managers must have the courage to break into the unknown
随机推荐
Explain detailed explanation and practice
一个对象引用的思考
How to dynamically add script dependent scripts
[Skill] Using Sentinel to achieve priority processing of requests
Use Patroni callback script to bind VIP pit
2022年PMP考试延迟了,该喜该忧?
Chapter 5 C programming expert thinking 5.4 alert Interpositioning of links
获取单选框选中内容
Converts XML tags to TXT format (voc conversion for yolo convenient training)
C专家编程 第5章 对链接的思考 5.6 轻松一下---看看谁在说话:挑战Turning测验
[Cloud Native--Kubernetes] Pod Resource Management and Probe Detection
Dynamic programming of the division of numbers
信息学奥赛一本通 1312:【例3.4】昆虫繁殖
2022软件测试面试题 最新字节跳动50道真题面试题 刷完已拿下15k 附讲解+答疑
Structure function exercise
C专家编程 第4章 令人震惊的事实:数组和指针并不相同 4.5 数组和指针的其他区别
【C语言进阶】程序环境和预处理
字节最爱问的智力题,你会几道?
某母婴小程序加密参数解密
Gartner 权威预测未来4年网络安全的8大发展趋势