动态规划(Dynamic Programming)
- 递归+记忆化 —> 递推
- 状态的定义:opt[n], dp[n], fib[n]
- 状态转移⽅方程:opt[n] = best_of(opt[n-1], opt[n-2], ...) 4. 最优⼦子结构
斐波那契数列列
0, 1, 1, 2, 3, 5, 8, 13, 21, ...
递推公式:F[n] = F[n-1] + F[n-2]
int fib(int n) {
return n <= 1 ? n : fib(n - 1) + fib(n - 2)
}
递归 + 记忆化 ==> 递推
递推公式: F[n] = F[n - 1] + F[n -2]
F[0] = 0, F[1] = 1
for (int i = 2; i <= n; ++i) {
F[i] = F[i - 1] + F[i - 2]
}
动态规划 vs 回溯 vs 贪⼼心算法
- 回溯(递归)— 重复计算
- 贪⼼算法 — 永远局部最优
- 动态规划 — 记录局部最优子结构 / 多种记录值
实战题⽬
- https://leetcode.com/problems/climbing-stairs/description/
- https://leetcode.com/problems/triangle/description/
- https://leetcode.com/problems/maximum-product-subarray/description/
- https://leetcode.com/problems/best-time-to-buy-and-sell-stock/#/description
- https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
- https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/
- https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/
- https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/
- https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/
- https://leetcode.com/problems/longest-increasing-subsequence
- https://leetcode.com/problems/coin-change/ 12.https://leetcode.com/problems/edit-distance/
← 位运算基础