One Pager Cheat Sheet
- This lesson will help you demystify and learn the skill of
Dynamic Programming
by walking through examples and providing a system to help solve most problems. Dynamic Programming
helps us save time by remembering information.- Using the
FAST
method can help us break down complex problems into smaller subproblems, such as calculating then
th Fibonacci number. - The Fibonacci sequence is a recursive formula where each number is the sum of the previous
2
numbers. - We need to
find the brute force solution
for the nth Fibonacci number as the first step of the FAST method before we can optimize it. - Our solution uses Dynamic Programming because it has both overlapping subproblems and an optimal substructure.
- We identify the subproblems as
fib(n-1)
andfib(n-2)
in order tomemoize
them and save results. - We are creating a
cache
to store already computed solutions and returning them if they exist, leading to a time complexity ofO(n)
and a space complexity ofO(n)
. - We will
iteratively
build from the base case andwork our way up
to solve the problem. - The bottom-up solution has an
O(n)
time and space complexity, while being easier to understand and more intuitive than the top-down solution. - Dynamic programming is an algorithm that solves complex problems by breaking them down into smaller subproblems and combining the solutions to obtain the optimal results, and it can be done either via a top-down or bottom-up
approach
. - The FAST method of dynamic programming
trades off
thetime complexity
of the algorithm forincreased space complexity
by allocating additional space to store data, thereby reducing the amount of time required to solve the problem. - By using a bottom-up approach to consider how much money can be made
robbing 0 to n houses
, theHouse Robber
problem can be solved to find the maximum amount of money that robbers can make. - We would
rob
onlyone house
and take what it has. If house A has more money, we rob house A; otherwise, we rob house B.
- The
bottom-up (iterative)
process of dynamic programming can be utilized to solve for thenth
answer by solving smaller subproblems for theith
answer. - The code
put together
would look likethis
. - The
bottom-up
approach of solving simpler cases and building an arrayarr
to store the max amount of money we can rob up to and including the current house ultimately allows us to find the maximum amount of money that can be robbed. - Dynamic programming allows for problems to be broken down into smaller subproblems and solutions from these subproblems to be
stored
and used to solve the larger problem, allowing for solutions without the need for knowledge of previous and current states, such as calculating the maximum amount of money we can rob for each house. - With
FAST
and enough practice, Dynamic Programming can be used to reach an optimal solution. - Using dynamic programming, the problem can be solved by initializing an array, looping from 0 to
n
, adding the base case, and filling the array using the recursive formula.