From 65c6c74cc457aabc87dd8d411a32a88121cab8bf Mon Sep 17 00:00:00 2001 From: Sainnhe Park Date: Wed, 8 Feb 2023 12:20:15 +0800 Subject: [PATCH] Add dp --- notes/src/SUMMARY.md | 5 +++++ notes/src/dynamic-programming-basic.md | 12 ++++++++++++ notes/src/dynamic-programming.md | 11 +++++++++++ 3 files changed, 28 insertions(+) create mode 100644 notes/src/dynamic-programming-basic.md create mode 100644 notes/src/dynamic-programming.md diff --git a/notes/src/SUMMARY.md b/notes/src/SUMMARY.md index b65a080..239ee94 100644 --- a/notes/src/SUMMARY.md +++ b/notes/src/SUMMARY.md @@ -51,6 +51,11 @@ - [总结](./greedy.md) +# 动态规划 + +- [总结](./dynamic-programming.md) +- [基础问题](./dynamic-programming-basic.md) + # STL - [总结](./stl.md) diff --git a/notes/src/dynamic-programming-basic.md b/notes/src/dynamic-programming-basic.md new file mode 100644 index 0000000..b30ec73 --- /dev/null +++ b/notes/src/dynamic-programming-basic.md @@ -0,0 +1,12 @@ +# 基础问题 + +## [509. 斐波那契数](https://leetcode.cn/problems/fibonacci-number/) + +1. `dp[i]` 是第 `i` 个斐波那契数的数值 +2. `dp[i] = dp[i - 1] + dp[i - 2]` +3. `dp[0] = 0`, `dp[1] = 1` +4. 从前向后遍历 + +```cpp + +``` diff --git a/notes/src/dynamic-programming.md b/notes/src/dynamic-programming.md new file mode 100644 index 0000000..506b827 --- /dev/null +++ b/notes/src/dynamic-programming.md @@ -0,0 +1,11 @@ +# 总结 + +和贪心相似,从将问题划分为多个子问题,最后得出最终问题的解,但是区别在于每一步之间涉及到状态推导,下一步是基于上一步的结果和之前的记忆(也就是上上次,上上上次等的结果)经过一定逻辑推导得出的。 + +分五步: + +1. 确定 `dp[i]` 是什么 +2. 确定递推公式 +3. `dp` 数组如何初始化 +4. 确定遍历顺序(从前向后还是从后向前) +5. 推几个来验证