您的位置:首页 > 其它

动态规划入门

2016-12-25 00:16 197 查看

(1)用 DP 做的题大多数返回值是int/boolean, 求max/min,不能打乱原来输入顺序。

(2)动态规划有两个重要定义,一个叫 "optimal substructure",另一个叫 "overlap subproblem".

各种排序 / Tree 类问题中,都会用到 divide & conquer 的思想,去把问题分成若干个 "disjoint" subproblems,然后递归解决。

"Disjoint" subproblem 在 Tree 类问题上体现的最为明显,左子树是左子树的问题,右子树是右子树的问题。因此 Tree 类问题上,更多的是解决“disjoint subproblem 的整合” 还有 “非连续 subproblem 的处理”。
而动态规划的中心思想是,面对 search tree 里都是 "overlap subproblem",如何根据问题结构制定缓存策略,避免重叠问题重复计算。

(3) 根据CLRS,动态规划分为两种:

top-down with memoization (递归记忆化搜索)

等价于带缓存的,搜索树上的 DFS

比较贴近于新问题正常的思考习惯

bottom-up (自底向上循环迭代)

以 "reverse topological order" 处理

每个子问题下面依赖的所有子问题都算完了才开始计算当前

一般依赖于子问题之间天然的 "size" 联系

两种解法 big-O 时间复杂度一致,bottom-up 的 constant factor 小一些。

(4) 动态规划的另一个要素是 "optimal substructure":


A problem is said to have optimal substructure if an optimal solution can be constructed efficiently from optimal solutions of its subproblems.

optimal substructure 指,一个问题的最优解,可以由其子问题的最优解构造出来。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: