Introduction to Dynamic Programming
Already, fib(5) is being called and recomputed 3 different times. To speed up our naive solution, we can employ DP by computing fib() for each value up to n=10 and storing it for future use. One way we can implement this is to slightly modify our original recursive function to check a table to see if we’ve already computed that specific Fibonacci number. If we have, we use it, otherwise we compute and then store it. This is the top-down approach(also called memoization) because start from the overall problem and recurse down until we reach the smallest subproblems (the base cases).