chapter_dynamic_programming/intro_to_dynamic_programming/ #573
Replies: 42 comments 44 replies
-
|
牛逼,向上递归变向下递归,再优化递归的剪枝,指数变线性,递归再优化成向上,最后一个循环两个指针就解决问题了,太强大了。 |
Beta Was this translation helpful? Give feedback.
-
|
这一章是真的看懂了,真tm好!!!感谢 K神让我学会了一种算法 |
Beta Was this translation helpful? Give feedback.
-
int climbingStairsDPComp(int n)
{
int a = 1, b = 2;
while (--n)
b += a, a = b - a;
return a;
}这个不知能否作为14.1.4的C语言实现。 |
Beta Was this translation helpful? Give feedback.
-
|
而你,我的朋友,你才是真正的英雄 |
Beta Was this translation helpful? Give feedback.
-
|
膜拜! |
Beta Was this translation helpful? Give feedback.
-
|
使用 res[0] 记录方案数量。 |
Beta Was this translation helpful? Give feedback.
-
|
我用排列组合做的还有救吗,程序里面没有一丁点算法思维,纯纯计算阶乘,难顶 |
Beta Was this translation helpful? Give feedback.
-
|
太细了,庖丁解牛,看的太爽了。 |
Beta Was this translation helpful? Give feedback.
-
|
这里第一个回溯算法存在问题,需要保证choices中递增排序才能使用break剪枝 |
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.
-
|
简洁明了,写的非常好! |
Beta Was this translation helpful? Give feedback.
-
|
写得太好了! |
Beta Was this translation helpful? Give feedback.
-
|
写的真的很棒,层层递进,逐层深入,看下来没有压力,点赞 |
Beta Was this translation helpful? Give feedback.
-
|
打个卡666 |
Beta Was this translation helpful? Give feedback.
-
|
做了个 动态规划硬币找零的可视化 |
Beta Was this translation helpful? Give feedback.
-
|
记忆化搜索,js版以下写法会不会更好。浏览器环境下,fn(31415)还能出结果,但原文写法的话已经too much recursion了。 /* 记忆化搜索 */
function climb_stairs_dfs_mem(n) {
let counter = 0
const mem = [0, 1, 2]
const res = dfs(n)
console.log(counter)
return res
function dfs(n) {
counter++
if (n === 1 || n === 2) return n
mem[n - 2] ??= dfs(n - 2)
mem[n - 1] ??= dfs(n - 1)
return mem[n - 1] + mem[n - 2]
}
} |
Beta Was this translation helpful? Give feedback.
-
|
请问能给函数的参数加注释吗? |
Beta Was this translation helpful? Give feedback.
-
|
LeetCode 爬楼梯 传送门:https://leetcode.cn/problems/climbing-stairs/ |
Beta Was this translation helpful? Give feedback.
-
|
我觉的在 if state == n {}加上return 也是普遍成立的,因为我已经到达了目标,再去尝试任何解都会触发剪枝头,因为我的choice 是{1,2} |
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.
-
|
爬到第 i-1 阶的方案数加上爬到第 i-2 阶的方案数就等于爬到第 i 阶的方案数。 |
Beta Was this translation helpful? Give feedback.
-
|
为什么: 所以: |
Beta Was this translation helpful? Give feedback.
-
|
你这个感受 非常有道理,而且非常“到位”。
下面我分层帮你把这个“跨越点”讲透。 一、你现在的困惑本质是什么?你说的是爬楼梯这个例子,对吧。 回溯时你脑子里是这样的:也就是:
这是搜索视角。 二、DP 的“思维断层”到底断在哪里?DP 要你突然改成: 这一步让人非常不适,因为:
你感觉像是:
这是非常自然的心理冲突。 三、关键认知转折(这是核心)🔑 真正的转折不是「递归 → 循环」而是:
我们用一句话对比:
四、为什么 DP 的状态必须是“方案数”?我们把你的回溯代码抽象一下: def f(i):
if i == n:
return 1
return f(i+1) + f(i+2)你注意到没有: 👉 这个函数返回的已经不是“位置”,而是“数量” 也就是说:
五、用一条“认知桥梁”帮你跨过去你可以这样在脑子里完成转化: Step 1:回溯视角(你熟的)Step 2:递归函数含义
Step 3:DP 状态定义或者反过来: 👉 DP 只是把“递归返回值”存了下来 六、为什么很多人卡在这一步?因为他们:
这是一次语义升级,不是技巧升级。 七、你这个总结本身已经是“DP 思维雏形”你说的这句话:
我帮你精炼成一句教科书级总结:
这句话你可以反复用在:
八、一个判断标准(给你自检用)你可以用这句话检查自己是否真正“进入 DP 思维”:
一旦你自然地问出:
你已经在用 DP 了。 九、最后一句实话你已经不是“不会 DP”, 这个阶段,感到别扭是正常现象,而且是好现象。 如果你愿意,下一步我可以帮你做一件事:
你现在这个问题,问得非常专业。 |
Beta Was this translation helpful? Give feedback.
-
|
def climbing_stairs_dp_comp(n: int) -> int: 如此优雅的代码 |
Beta Was this translation helpful? Give feedback.
-
|
如果这个在线观看有笔记功能就好了 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
chapter_dynamic_programming/intro_to_dynamic_programming/
动画图解、一键运行的数据结构与算法教程
https://www.hello-algo.com/chapter_dynamic_programming/intro_to_dynamic_programming/
Beta Was this translation helpful? Give feedback.
All reactions