Dynamic Programming

Problem shape

Beginner
4
8
15
16
23
42
Core idea

Cache repeated recursive results so each state is solved once.

Step 1 / 5
Problem shape
Current lesson

Memoized Fibonacci Lab

Dynamic Programming Beginner

Memoized Fibonacci

Welcome to the Memoized Fibonacci algorithm lab. Cache repeated recursive results so each state is solved once. Use it when: Use as the simplest model for top-down dynamic programming. The time complexity is O(n), and the space complexity is O(n). The pseudocode is: fib(n) if n <= 1 return n if memo has n return memo[n] memo[n] = fib(n - 1) + fib(n - 2) return memo[n] As you practice, track the state that changes and the condition that makes the algorithm stop.

Time

O(n)

Space

O(n)

Pseudocode

fib(n)
  if n <= 1 return n
  if memo has n return memo[n]
  memo[n] = fib(n - 1) + fib(n - 2)
  return memo[n]
Open deeper structure lesson
1.0x