Skip to main content

Command Palette

Search for a command to run...

πŸ“Š Dynamic Programming Explained Like You're 5

Published
β€’2 min read
S

Building AI systems and writing about how they actually work. Master of AI @ University of Technology Sydney. Previously B.Tech CS with focus on IoT. I believe the best way to learn is to explain. That's why I'm documenting tech concepts with simple analogies (@sreekarreddy.com). AWS Certified β€’ Azure AI Certified β€’ Neo4j Professional β€’ Google Data Analytics When not coding: exploring Sydney, working on side projects, and teaching tech to anyone who'll listen.

Solving problems by remembering past results

Day 93 of 149

πŸ‘‰ Full deep-dive with code examples


The Homework Analogy

Doing homework, you keep solving the same sub-problem:

Dumb approach: Recalculate every time Smart approach: Write down answers, look them up!

Dynamic Programming = remembering solutions you've already computed.


The Classic Example: Fibonacci

# Bad: Recalculates the same values constantly
def fib(n):
    if n <= 1: return n
    return fib(n-1) + fib(n-2)  # fib(3) calculated 10x!

# Good: Remember what we calculated
def fib(n, memo={}):
    if n in memo: return memo[n]  # Already solved!
    if n <= 1: return n
    memo[n] = fib(n-1, memo) + fib(n-2, memo)
    return memo[n]

From O(2^n) β†’ O(n)! Exponentially faster!

For larger n, this is typically much faster.


When to Use DP

Two conditions:

  1. Overlapping subproblems: Same calculations repeat
  2. Optimal substructure: A good overall solution can be built from good sub-solutions

Famous DP Problems

ProblemDescription
FibonacciSum of previous two
KnapsackMaximize value with weight limit
Edit DistanceMinimum edits to transform string
Coin ChangeMinimum coins for amount

The Power

Without DP: Some problems can take a very long time With DP: The same problems can become fast enough to run in practice


In One Sentence

Dynamic Programming solves problems by storing solutions to sub-problems and reusing them.


πŸ”— Enjoying these? Follow for daily ELI5 explanations!

Making complex tech concepts simple, one day at a time.

More from this blog

esreekarreddy

132 posts