chapter_dynamic_programming/intro_to_dynamic_programming/ #573
Replies: 32 comments 33 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.
-
使用 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.
-
climbing_stairs_backtrack.java
if (state == n) {
res.set(0, res.get(0) + 1);
return;
} 我试了几个数据,这里加上return后结果没有改变,而且运行速度更快,请问这样子加上return会有什么隐患嘛(作者这里没有加return是有什么考虑嘛) |
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.
-
这个问题莫名有点像上一章《回溯》里面的《子集》问题,给定一个集合[3,4,5],求他们和是9的子集有多少种。这个问题换成集合[1,2],和是n的子集。不同于前面要考虑去重,这里不用考虑。{1,2}、{2,1}被认为是不同的结果。那我很好奇,当然dfs也很不错,但是如果上楼梯的步数变成[1,2,3],是不是就变成dfs(i)=dfs(i-1)+dfs(i-2)+dfs(i-3)。 |
Beta Was this translation helpful? Give feedback.
-
牛啊,循循渐进,先从回溯法切入问题,再一步一步优化,对我这个初学者来说很好理解 |
Beta Was this translation helpful? Give feedback.
-
C++ /* 回溯 */
void backtrack(vector<int> &choices, int state, int n, vector<int> &res) {
// 当爬到第 n 阶时,方案数量加 1
bool isDirty = false;
if (state == n) {
res[0]++;
isDirty = true;
}
// 遍历所有选择
if (!isDirty) {
for (auto &choice : choices) {
// 剪枝:不允许越过第 n 阶
if (state + choice > n)
continue;
// 尝试:做出选择,更新状态
backtrack(choices, state + choice, n, res);
// 回退
}
}
} |
Beta Was this translation helpful? Give feedback.
-
妙啊!!,真的是太妙了,大哥哥,我是小学生,tql |
Beta Was this translation helpful? Give feedback.
-
k神,看到这里突然对动态规划和分治有些模糊,都是将大问题拆分为小问题。我的理解:分治拆分的小问题是独立的,不重复的,未知的,所以需要有“合并”的操作,所以需要递归。而动态规划的小问题具有重复性,已知的,所以可以采用迭代。这样理解正确吗? |
Beta Was this translation helpful? Give feedback.
-
好像还没人发现,14.1.4 Rust 代码可以用解构省去 tmp 变量: /* 爬楼梯:空间优化后的动态规划 */
fn climbing_stairs_dp_comp(n: usize) -> i32 {
if n == 1 || n == 2 {
return n as i32;
}
let (mut a, mut b) = (1, 2);
for _ in 3..=n {
(a, b) = (b, a+b);
}
b
} |
Beta Was this translation helpful? Give feedback.
-
写的真的很棒,层层递进,逐层深入,看下来没有压力,点赞 |
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.
-
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