chapter_dynamic_programming/dp_solution_pipeline/ #589
Replies: 19 comments 28 replies
-
for (int i = 1; i < n; i++) { |
Beta Was this translation helpful? Give feedback.
-
太厉害了!!!!!,义父,请受孩儿一拜 |
Beta Was this translation helpful? Give feedback.
-
总结: 一个小小疑惑: 如果我理解的没错的话,这里应该是指: |
Beta Was this translation helpful? Give feedback.
-
这个空间优化实在太妙了。 每一行的遍历开始的时候:dp[0] 是新一行的值,而dp[i] 是上一行的值,由此将其全部推导为当前行的值 |
Beta Was this translation helpful? Give feedback.
-
图 14-16 的第2列貌似错了 |
Beta Was this translation helpful? Give feedback.
-
请教!DP问题如果可以列出DP表,看起来就包含着问题的解,那我们实际解决问题的时候比较确定这是一题符合DP问题特点的题目时,是否可以直接考虑使用DP表直接写出最终答案,而不需要考虑暴力搜索等其他解?因为好像有时候反而不容易想到要如何暴力搜索,而是很强烈的感觉这题用dp表可以做。 |
Beta Was this translation helpful? Give feedback.
-
您好,我问一下:for i in range(1, n): |
Beta Was this translation helpful? Give feedback.
-
如果右上的5和2都换成1,这个每次选最小的,出来的路线就不不对了,这样直接走两条直角边总和才10 |
Beta Was this translation helpful? Give feedback.
-
文中的先观察问题是否适合使用回溯(穷举)解决这句话感觉可以调整成是否适合使用回溯(自上向下穷举)解决,因为我一开始用自下向上的回溯发现没法用记忆搜索优化,导致推导不出后面的问题,哈哈,可能是我太菜了 |
Beta Was this translation helpful? Give feedback.
-
复杂的算法能从浅入深,作者的功力太强了,算法这种就是做出来容易讲出来让别人理解难 |
Beta Was this translation helpful? Give feedback.
-
k神能省则省,这思路太牛了 |
Beta Was this translation helpful? Give feedback.
-
给大佬提一个可能不合理的小建议,如果在代码之前写一下输入和输出是什么,可能会更容易让凡人理解。 |
Beta Was this translation helpful? Give feedback.
-
import torch
cost = torch.tensor([[1,3,1,5],
[2,2,4,2],
[5,3,2,1],
[4,3,5,2]])
dp = torch.ones_like(cost)*torch.inf
dp[0,0] = 1
for i in range(4):
for j in range(4):
last_i = i-1
last_j = j-1
if last_i>=0 and last_j>=0:
dp[i,j] = torch.min(dp[last_i,j], dp[i,last_j]) + cost[i,j]
elif last_i>=0 and last_j<0:
dp[i, j] = dp[last_i, j] + cost[i, j]
elif last_i<0 and last_j>=0:
dp[i, j] = dp[i, last_j] + cost[i, j]
else:
pass
print(dp[3,3]) 终于好像有get到一点点动态规划了,人生第一个动态规划的代码! |
Beta Was this translation helpful? Give feedback.
-
感觉动态规划和强化学习好像啊。 强化学习里面也有初始状态、终止状态、马尔科夫性、状态转移方程。 或者是否能说DP和RL都是用来解决序列决策问题的方法,不过DP要求问题的状态转移方程已知;而RL则通过与环境互动学习各个状态的最优决策,不需要状态转移方程。 另外有一个问题,DP问题中,如何对状态转移进行剪枝呢?换句话说,如何对设计合适的状态转移方法,尽可能少地探索状态以到达我们的目标状态? |
Beta Was this translation helpful? Give feedback.
-
Hi,我有些疑问。如果我有个4*4的表格
如果我要求从左上角到(2,2)的最小路径和,根据这个算法得到的结果是4,没有问题。 |
Beta Was this translation helpful? Give feedback.
-
回溯算法不是需要做出选择与回退选择吗,看代码里没有体现呢 |
Beta Was this translation helpful? Give feedback.
-
这小节的空间优化还是有点绕。很巧妙。 |
Beta Was this translation helpful? Give feedback.
-
// 若为左上角单元格,则终止搜索 |
Beta Was this translation helpful? Give feedback.
-
a=[[1,1,2,3],
[10,1,0,0]
[1,1,2,1]] k神你好,对于其中dp[2,0]的值按照初始化为12,但是按照最小代价来看,不应该是1+1+1+1+1为5 |
Beta Was this translation helpful? Give feedback.
-
chapter_dynamic_programming/dp_solution_pipeline/
动画图解、一键运行的数据结构与算法教程
https://www.hello-algo.com/chapter_dynamic_programming/dp_solution_pipeline/
Beta Was this translation helpful? Give feedback.
All reactions