From 18a5fbd75c95b7c924d382560df5baace53e5595 Mon Sep 17 00:00:00 2001 From: marina <138340846+bt3gl-cryptographer@users.noreply.github.com> Date: Fri, 4 Aug 2023 18:47:47 -0700 Subject: [PATCH] Update README.md --- dynamic_programming/README.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/dynamic_programming/README.md b/dynamic_programming/README.md index debe8c9..3446e2b 100644 --- a/dynamic_programming/README.md +++ b/dynamic_programming/README.md @@ -50,9 +50,30 @@ def reverse(s):
* memoization is an optimization technique that avoids recursion's duplicate calculations. + * it's primarily used to speed up code by storing the intermediate results in a cache so that it can be reused later. * for example, a hash table can be used as a cache and should be passed along each subroutine call. -* classic examples are fibonacci and the "climbing stairs" problem: +* here is a basic template with pseudo-code: + +```python +function dp(dp_state, memo_dict): + + if dp_state is the base cases + return things like 0 or null + + if dp_state in memo_dict + return memo_dict[dp_state] + + calculate dp(dp_state) from dp(other_state) + save dp_state and the result into memo_dict + +function answerToProblem(input) + return dp(start_state, empty_memo_dict) +``` + +
+ +* classic examples where memoization can be used are fibonacci and the "climbing stairs" problem: ```python cache = {1: 1, 0: 1}