(1)用 DP 做的題大多數返回值是int/boolean, 求max/min,不能打亂原來輸入順序。
(2)動態規劃有兩個重要定義,一個叫 "optimal substructure",另一個叫 "overlap subproblem".
"Disjoint" subproblem 在 Tree 類問題上體現的最爲明顯,左子樹是左子樹的問題,右子樹是右子樹的問題。因此 Tree 類問題上,更多的是解決“disjoint subproblem 的整合” 還有 “非連續 subproblem 的處理”。
而動態規劃的中心思想是,面對 search tree 裏都是 "overlap subproblem",如何根據問題結構制定緩存策略,避免重疊問題重複計算。
(3) 根據CLRS,動態規劃分爲兩種:
-
top-down with memoization (遞歸記憶化搜索)
-
等價於帶緩存的,搜索樹上的 DFS
-
比較貼近於新問題正常的思考習慣
-
-
bottom-up (自底向上循環迭代)
-
以 "reverse topological order" 處理
-
每個子問題下面依賴的所有子問題都算完了纔開始計算當前
-
一般依賴於子問題之間天然的 "size" 聯繫
-
(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 指,一個問題的最優解,可以由其子問題的最優解構造出來。