chapter_dynamic_programming/dp_problem_features/ #575
Replies: 26 comments 26 replies
-
关于子问题是否独立的疑问:
而在本文中提到
有点迷惑了。DP 的子问题到底是不是相互独立的呢? |
Beta Was this translation helpful? Give feedback.
-
所以动态规划是一种特殊的回溯算法吗,回溯算法加上记忆存储优化在改写为循环 |
Beta Was this translation helpful? Give feedback.
-
14.1 和 14.2 章看下来,动态规划给我的感觉更像是解数学题。解决问题的关键是找到「状态转移方程」,也就是递推公式。而代码只是在找到这个递推关系后不断循环递推到问题的解。所以我猜难点其实在如何把问题拆分成子问题来找到这个递推关系。只要找到递推关系后,剩下的工作基本就交给计算机里。而在有约束的情况下,递推关系会变成好几组式子。但只要是线性的关系其实都还好,总可以写成矩阵乘法的形式。 比如图14-9的「状态转移方程」: dp[i,1]=dp[i-1,2] 其实等价于: 而对于非线性的情况下就可能会比较复杂,如果没有办法写成[DP(i)]的次幂形式的话,可能还是得一个个分量来写。 |
Beta Was this translation helpful? Give feedback.
-
第一个案例的 Swift 版本代码有误: |
Beta Was this translation helpful? Give feedback.
-
请问是否可以这样理解后无效性? |
Beta Was this translation helpful? Give feedback.
-
原文: |
Beta Was this translation helpful? Give feedback.
-
if (n == 1 || n == 2) |
Beta Was this translation helpful? Give feedback.
-
为什么初始状态是这样?不理解 |
Beta Was this translation helpful? Give feedback.
-
状态[i,j]表示处在第i阶并且上一轮跳了j阶 dp[1][1] = 1;
dp[1][2] = 0;
dp[2][1] = 0;
dp[2][2] = 1 按照定义,dp[1][1]表示处在第 1 阶上并且上一轮跳了 1 阶.但是题目限定不能连续两轮跳 1 阶.那么dp[1][1]应该等于 0 吧.这里不是很理解,求解答 |
Beta Was this translation helpful? Give feedback.
-
重叠子问题、最优子结构、无后效性 |
Beta Was this translation helpful? Give feedback.
-
dp[2][1] = 0; 为什么是 0,感觉应该是 1 啊 |
Beta Was this translation helpful? Give feedback.
-
最后约束爬楼梯这里不太理解: |
Beta Was this translation helpful? Give feedback.
-
14.2.1这个不太对啊 |
Beta Was this translation helpful? Give feedback.
-
dp = [[0] * 3 for _ in range(n + 1)] 大大您好,请问这边为什么是[0]*3而不是[0]*2,谢谢您! |
Beta Was this translation helpful? Give feedback.
-
纸质书中的爬楼梯约束问题有错误: |
Beta Was this translation helpful? Give feedback.
-
真是太狗了,佩服的五体投地,为什么可以想出扩展状态,我看着惊掉了我的下巴...,真是应了那句话,在软件工程中,没有什么是加一层解决不了的,如果....那就 |
Beta Was this translation helpful? Give feedback.
-
动态规划中的无后效性一词出自哪里? 查了维基百科和算法导论(第15章)上对于动态规划的描述,都没有发现无后效性一词(可能我看漏了)。 |
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.
-
这不就是马尔可夫链吗 ╮( ̄▽ ̄")╭ |
Beta Was this translation helpful? Give feedback.
-
不允许连跳两次1阶这题,初始状态设置是这样理解的吧: const dp = [
/* [无用仅占位, 跳1阶到此的解数, 跳2阶到此的解数] */
[null, null, null],//0阶解,无用仅占位
[null, 1, 0],//1阶解 = 跳1阶可达 + 跳2阶不可达
[null, 0, 1],//2阶解 = 跳1阶可达但违规(因为此时前一次只能也是跳1阶) + 跳2阶可达
] 因此,以后的状态设置: dp[i] = [
null,
dp[i - 1][2],
dp[i - 2][1] + dp[i - 2][2]
] |
Beta Was this translation helpful? Give feedback.
-
2i屏障这题,只观察到以下:
|
Beta Was this translation helpful? Give feedback.
-
不能连续两轮跳一阶的问题,状态转移方程也可以是 def climbing_stairs_constraint_dp(n: int) -> int:
dp = [0, 1, 1, 2]
if n <= 3:
return dp[n]
for i in range(4, n+1):
dp[1], dp[2], dp[3] = dp[2], dp[3], dp[1] + dp[2]
return dp[3] |
Beta Was this translation helpful? Give feedback.
-
"最小成本爬楼梯"对应lc#746. Min Cost Climbing Stairs,有一个经典的歧义。 /* DP 超越i阶 */
function minCostClimbingStairs(cost) {
const dp = [0, 0]
for (let i = 2; i <= cost.length; i++) {
/* dp[i]定义:超过i级台阶(i级台阶本身不一定被踩到)所需最小成本 */
dp[i] = Math.min(
dp[i - 2] + cost[i - 2],
dp[i - 1] + cost[i - 1],
)
}
console.log(dp) //[0,0,10,15,30,40,60,75]
return dp.at(-1) //75
} /* DP 到达i阶 */
function min_cost_climbing_stairs_dp(cost) {
const dp = [0, cost[0], cost[1]]
for (let i = 3; i < cost.length; i++) {
// 设 dp[i] 为爬到第i阶累计付出的代价(包含第i阶本身,即第i阶必须被"踩到"),
// 由于第i阶只可能从i-1阶或i-2阶走来,
// 因此 dp[i] 只可能等于 dp[i-1]+cost[i] 或 dp[i-2]+cost[i] 。
dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i]
/* 最优子结构的含义:原问题的最优解是从子问题的最优解构建得来的。 */
}
console.log(dp)//[0,10,15,35,45,70,85]
return dp.at(-1)//85
} |
Beta Was this translation helpful? Give feedback.
-
chapter_dynamic_programming/dp_problem_features/
动画图解、一键运行的数据结构与算法教程
https://www.hello-algo.com/chapter_dynamic_programming/dp_problem_features/
Beta Was this translation helpful? Give feedback.
All reactions